changeset 6:c55a04d93b87 default tip

Fix one more bug in mini_fnmatch Fix greediness, recursive version still affected by the bug.
author Louis Opter <kalessin@kalessin.fr>
date Fri, 27 Dec 2013 08:06:33 -0800
parents 7f981c321063
children
files arrays/mini_fnmatch/solution.py
diffstat 1 files changed, 17 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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')