Skip to content

Commit 0ca7380

Browse files
prateek3255MadhavBahl
authored andcommitted
added boyer moore and rabin karp in python (#134)
1 parent a0a245c commit 0ca7380

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

day12/Python/boyerMoore.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
@author prateek3255
3+
@date 05/01/2018
4+
'''
5+
def boyerMoore(string,pattern):
6+
n=len(string)
7+
m=len(pattern)
8+
i=0
9+
while i<=n-m:
10+
k=m-1
11+
j=m+i-1
12+
while string[j]==pattern[k]:
13+
k=k-1
14+
j=j-1
15+
if k==0:
16+
return i
17+
if pattern.rfind(string[j])==-1:
18+
i=j+1
19+
else:
20+
i=max(1,j-pattern.rfind(string[j]))
21+
return -1
22+
23+
print(boyerMoore("helloworld","hello"))
24+
print(boyerMoore("helloworld","hop"))
25+
print(boyerMoore("abcrxyzgf","xyz"))
26+
print(boyerMoore("ABABDABACDABABCABAB","ABABCABAB"))

day12/Python/rabinKarp.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
@author prateek3255
3+
@date 05/01/2018
4+
'''
5+
6+
def rabinKarp(string,pattern,q=153):
7+
n=len(string)
8+
m=len(pattern)
9+
d=256
10+
h=d**(m-1)%q
11+
p=0
12+
t=0
13+
14+
for i in range(0,m):
15+
p=(d*p+ord(pattern[i]))%q
16+
t=(d*t+ord(string[i]))%q
17+
for i in range(n-m+1):
18+
if p==t and string[i:i+m]==pattern:
19+
return i
20+
21+
if i<(n-m):
22+
t= (d*(t-ord(string[i])*h)+ord(string[i+m]))%q
23+
if t<0:
24+
t+=q
25+
return -1
26+
27+
print(rabinKarp("helloworld","hello"))
28+
print(rabinKarp("helloworld","hop"))
29+
print(rabinKarp("ABABDABACDABABCABAB","ABABCABAB"))
30+
31+
32+
33+

day12/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,43 @@ To Be Added
457457
To Be Added
458458
```
459459

460+
### Python Implementation
461+
462+
#### [Solution](./Python/rabinKarp.py)
463+
464+
```py
465+
'''
466+
@author prateek3255
467+
@date 05/01/2018
468+
'''
469+
470+
def rabinKarp(string,pattern,q=153):
471+
n=len(string)
472+
m=len(pattern)
473+
d=256
474+
h=d**(m-1)%q
475+
p=0
476+
t=0
477+
478+
for i in range(0,m):
479+
p=(d*p+ord(pattern[i]))%q
480+
t=(d*t+ord(string[i]))%q
481+
for i in range(n-m+1):
482+
if p==t and string[i:i+m]==pattern:
483+
return i
484+
485+
if i<(n-m):
486+
t= (d*(t-ord(string[i])*h)+ord(string[i+m]))%q
487+
if t<0:
488+
t+=q
489+
return -1
490+
491+
print(rabinKarp("helloworld","hello"))
492+
print(rabinKarp("helloworld","hop"))
493+
print(rabinKarp("ABABDABACDABABCABAB","ABABCABAB"))
494+
495+
```
496+
460497
## C++ Implementation
461498

462499
### [Solution](./C++/RabinKarp.cpp)
@@ -523,6 +560,39 @@ int main()
523560
524561
```js
525562
To Be Added
563+
```
564+
### Python Implementation
565+
566+
#### [Solution](./Python/boyerMoore.py)
567+
568+
```py
569+
'''
570+
@author prateek3255
571+
@date 05/01/2018
572+
'''
573+
def boyerMoore(string,pattern):
574+
n=len(string)
575+
m=len(pattern)
576+
i=0
577+
while i<=n-m:
578+
k=m-1
579+
j=m+i-1
580+
while string[j]==pattern[k]:
581+
k=k-1
582+
j=j-1
583+
if k==0:
584+
return i
585+
if pattern.rfind(string[j])==-1:
586+
i=j+1
587+
else:
588+
i=max(1,j-pattern.rfind(string[j]))
589+
return -1
590+
591+
print(boyerMoore("helloworld","hello"))
592+
print(boyerMoore("helloworld","hop"))
593+
print(boyerMoore("abcrxyzgf","xyz"))
594+
print(boyerMoore("ABABDABACDABABCABAB","ABABCABAB"))
595+
526596
```
527597

528598
### Have Another solution?

0 commit comments

Comments
 (0)