# HG changeset patch # User Louis Opter # Date 1388160393 28800 # Node ID c55a04d93b872a1d7096662884aec204d5863eb2 # Parent 7f981c3210639a1152987787adaebe3959aee88d Fix one more bug in mini_fnmatch Fix greediness, recursive version still affected by the bug. diff -r 7f981c321063 -r c55a04d93b87 arrays/mini_fnmatch/solution.py --- a/arrays/mini_fnmatch/solution.py Tue Dec 10 19:50:34 2013 -0800 +++ b/arrays/mini_fnmatch/solution.py Fri Dec 27 08:06:33 2013 -0800 @@ -19,9 +19,15 @@ nextchar_pat = None if pat_idx + 1 == len(pat) else pat[pat_idx + 1] if nextchar_pat is None: return True - if nextchar_pat == name[name_idx]: - pat_idx += 2 - name_idx += 1 + reverse_name_idx = namelen - 1 + while True: + if name[reverse_name_idx] == nextchar_pat: + name_idx = reverse_name_idx + 1 + pat_idx += 2 + break + if reverse_name_idx == name_idx: + return False + reverse_name_idx -= 1 elif name[name_idx] == pat[pat_idx]: name_idx += 1 pat_idx += 1 @@ -48,6 +54,8 @@ if __name__ == "__main__": assert fnmatch("toto.py", "*") print('assert fnmatch("toto.py", "*") → ok') + assert fnmatch("toto.py", "*to.py") + print('assert fnmatch("toto.py", "*to.py") → ok') assert fnmatch("toto.py", "*.py") print('assert fnmatch("toto.py", "*.py") → ok') assert not fnmatch("ab.pya", "*.py") @@ -75,10 +83,12 @@ assert fnmatch_r("toto.py", "*") print('assert fnmatch_r("toto.py", "*") → ok') + assert fnmatch_r("toto.py", "*to.py") + print('assert fnmatch_r("toto.py", "*to.py") → ok') assert fnmatch_r("toto.py", "*.py") print('assert fnmatch_r("toto.py", "*.py") → ok') - assert not fnmatch("ab.pya", "*.py") - print('assert not fnmatch("ab.pya", "*.py") → ok') + assert not fnmatch_r("ab.pya", "*.py") + print('assert not fnmatch_r("ab.pya", "*.py") → ok') assert not fnmatch_r("toto.py", "*.c") print('assert not fnmatch_r("toto.py", "*.c") → ok') assert fnmatch_r("toto.py", "toto.*") @@ -96,6 +106,6 @@ assert not fnmatch_r("aaa", "bbb") print('assert not fnmatch_r("aaa", "bbb") → ok') assert fnmatch_r("", "") - print('assert fnmatch("", "") → ok') + print('assert fnmatch_r("", "") → ok') assert not fnmatch_r("toto", "") - print('assert not fnmatch("toto", "") → ok') + print('assert not fnmatch_r("toto", "") → ok')