-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabin_karp.py
More file actions
51 lines (40 loc) · 1.26 KB
/
rabin_karp.py
File metadata and controls
51 lines (40 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Number of characters in input alphabet
D = 256
def search(pat, text, q):
global D
M = len(pat)
N = len(text)
pattern_hash = 0
text_hash = 0
h = 1
for i in range(M - 1):
print(h)
h = (h*D)%q
for i in range(M):
pattern_hash = (D * pattern_hash + ord(pat[i])) % q
text_hash = (D * text_hash + ord(text[i])) % q
print("{} => {}".format(pat, pattern_hash))
print("Searching text now!")
for i in range(N-M+1):
print("{} => {}".format(text[i:i+M], text_hash))
if pattern_hash == text_hash:
print("Hash matched!")
# Check both pattern and text one by one to confirm
for j in range(M):
if text[i+j] != pat[j]:
print("Hash matched but turned out to be not equal")
break
if j == M - 1:
print("Pattern found at {}".format(i))
return
else:
if i < N-M:
text_hash = (D * (text_hash - ord(text[i]) * h) + ord(text[i + M])) % q
if text_hash < 0:
text_hash += q
print("Pattern not found!")
def test():
text = "maryhadalittlelamb"
pattern = "had"
q = 101
search(pattern, text, q)