Permalink
Browse files

extended rabinkarp_search to support multiple substring matches

  • Loading branch information...
1 parent 0cebed6 commit 069027f123325e824c8292329e8135f59ef784a3 @jabagawee committed Sep 29, 2012
Showing with 16 additions and 15 deletions.
  1. +15 −14 algorithms/searching/rabinkarp_search.py
  2. +1 −1 algorithms/tests/test_searching.py
View
29 algorithms/searching/rabinkarp_search.py
@@ -9,28 +9,29 @@
Pre: two strings, one to search in and one to search for.
- Post: returns the index of the start of the search string.
+ Post: returns a list of indices where the substring was found
- Time Complexity: O(nm)
+ Time Complexity: O(nm)
Psuedo Code: http://en.wikipedia.org/wiki/Rabin-Karp_algorithm
- rabinkarp_search.search(searchString, targetString) -> integer
- rabinkarp_search.search(searchString, targetString) -> False
+ rabinkarp_search.search(searchString, targetString) -> list[integers]
+ rabinkarp_search.search(searchString, targetString) -> list[empty]
"""
-import hashlib
-
+from hashlib import md5
def search(s, sub):
n, m = len(s), len(sub)
- hsub, hs = hashlib.md5(sub), hashlib.md5(s[0:m])
-
- for i in range(n - m + 1):
- if hs.hexdigest() == hsub.hexdigest():
+ hsub = md5(sub)
+ offsets = []
+ if m > n:
+ return offsets
+
+ for i in xrange(n - m + 1):
+ hs = md5(s[i:i + m])
+ if hs.digest() == hsub.digest():
if s[i:i + m] == sub:
- return i
- news = s[i + 1:i + m + 1]
- hs = hashlib.md5(news)
+ offsets.append(i)
- return False
+ return offsets
View
2 algorithms/tests/test_searching.py
@@ -54,7 +54,7 @@ def test_rabinkarpsearch(self):
self.string = "ABCDEFGHIJKLMNOP"
rv1 = rabinkarp_search.search(self.string, "MNOP")
rv2 = rabinkarp_search.search(self.string, "BCA")
- self.assertIs(rv1, 12)
+ self.assertIs(rv1[0], 12)
self.assertFalse(rv2)

0 comments on commit 069027f

Please sign in to comment.