Browse files

Merge pull request #25 from jabagawee/rabinkarp_search

extended rabinkarp_search to support multiple substring matches
  • Loading branch information...
2 parents d473ec2 + 5bb5c4d commit e3db521421530250c44d6d3fae60d89b8b37f512 @nryoung committed Oct 2, 2012
Showing with 14 additions and 14 deletions.
  1. +13 −13 algorithms/searching/rabinkarp_search.py
  2. +1 −1 algorithms/tests/test_searching.py
View
26 algorithms/searching/rabinkarp_search.py
@@ -9,28 +9,28 @@
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])
+ hsub_digest = md5(sub).digest()
+ offsets = []
+ if m > n:
+ return offsets
- for i in range(n - m + 1):
- if hs.hexdigest() == hsub.hexdigest():
+ for i in xrange(n - m + 1):
+ if md5(s[i:i + m]).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 e3db521

Please sign in to comment.