Skip to content

Commit acbe6aa

Browse files
prateek3255Razdeep
authored andcommitted
added brute force and kmp in python (#129)
* added brute force and kmp in python * added author comment
1 parent 4c01223 commit acbe6aa

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

day12/Python/KMP.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
@author prateek3255
3+
@date 05/01/2018
4+
'''
5+
6+
def kmp(string,pattern):
7+
n=len(string)
8+
m=len(pattern)
9+
lps=calculateLPS(pattern)
10+
i=0
11+
j=0
12+
while i<n:
13+
if pattern[j]==string[i]:
14+
i+=1
15+
j+=1
16+
if j==m:
17+
return i-j
18+
elif i<n and pattern[j]!=string[i]:
19+
if j!=0:
20+
j=lps[j-1]
21+
else:
22+
i+=1
23+
return -1
24+
25+
def calculateLPS(pattern):
26+
lps=[0]*len(pattern)
27+
i=1
28+
lenPat=0
29+
while i<len(pattern):
30+
if pattern[i]==pattern[lenPat]:
31+
lenPat+=1
32+
lps[i]=lenPat
33+
i+=1
34+
else:
35+
if lenPat!=0:
36+
lenPat=lps[lenPat-1]
37+
else:
38+
lps[i]=0
39+
i+=1
40+
return lps
41+
42+
print(kmp("helloworld","hello"))
43+
print(kmp("helloworld","hop"))
44+
print(kmp("ABABDABACDABABCABAB","ABABCABAB"))
45+

day12/Python/bruteForce.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
@author prateek3255
3+
@date 05/01/2018
4+
'''
5+
6+
def subStringSearch(string,pattern):
7+
for i in range(len(string)-len(pattern)):
8+
if string[i:i+len(pattern)]==pattern:
9+
return i
10+
return -1
11+
12+
print(subStringSearch("helloworld","hello"))
13+
print(subStringSearch("helloworld","hop"))
14+
print(subStringSearch("abcrxyzgf","xyz"))

day12/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ substringSearch ("helloworld", "world");
7575
substringSearch ("abcrxyzgf", "xyz");
7676
```
7777

78+
### Python Implementation
79+
80+
#### [Solution](./Python/bruteForce.py)
81+
82+
```py
83+
'''
84+
@author prateek3255
85+
@date 05/01/2018
86+
'''
87+
88+
def subStringSearch(string,pattern):
89+
for i in range(len(string)-len(pattern)):
90+
if string[i:i+len(pattern)]==pattern:
91+
return i
92+
return -1
93+
94+
print(subStringSearch("helloworld","hello"))
95+
print(subStringSearch("helloworld","hop"))
96+
print(subStringSearch("abcrxyzgf","xyz"))
97+
```
98+
7899
## B) Knuth-Morris-Pratt Algorithm
79100

80101
### JavaScript Implementation
@@ -85,6 +106,53 @@ substringSearch ("abcrxyzgf", "xyz");
85106
To Be Added
86107
```
87108

109+
### Python Implementation
110+
111+
#### [Solution](./Pyhton/KMP.py)
112+
113+
```py
114+
def kmp(string,pattern):
115+
n=len(string)
116+
m=len(pattern)
117+
lps=calculateLPS(pattern)
118+
i=0
119+
j=0
120+
while i<n:
121+
if pattern[j]==string[i]:
122+
i+=1
123+
j+=1
124+
if j==m:
125+
return i-j
126+
elif i<n and pattern[j]!=string[i]:
127+
if j!=0:
128+
j=lps[j-1]
129+
else:
130+
i+=1
131+
return -1
132+
133+
def calculateLPS(pattern):
134+
lps=[0]*len(pattern)
135+
i=1
136+
lenPat=0
137+
while i<len(pattern):
138+
if pattern[i]==pattern[lenPat]:
139+
lenPat+=1
140+
lps[i]=lenPat
141+
i+=1
142+
else:
143+
if lenPat!=0:
144+
lenPat=lps[lenPat-1]
145+
else:
146+
lps[i]=0
147+
i+=1
148+
return lps
149+
150+
print(kmp("helloworld","hello"))
151+
print(kmp("helloworld","hop"))
152+
print(kmp("ABABDABACDABABCABAB","ABABCABAB"))
153+
```
154+
155+
88156
## C) Z Algorithm
89157

90158
### JavaScript Implementation

0 commit comments

Comments
 (0)