Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

extended rabinkarp_search to support multiple substring matches #25

Merged
merged 2 commits into from

2 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
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)
Something went wrong with that request. Please try again.