-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathnaive_search.py
36 lines (29 loc) · 887 Bytes
/
naive_search.py
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
# Implementation of naive search of substrings in string
def naive_search_while(string,sub):
str_len = len(string)
sub_len = len(sub)
for i in range(str_len-sub_len +1):
for j in range(sub_len):
if string[i+j] == sub[j]:
if j == sub_len--:
return i
else:
break
#naive approach reduces the time complexity
def naive_search_for(string,sub):
str_len = len(string)
sub_len = len(sub)
i = j = 0
while i <= str_len-sub_len and j < sub_len:
if string[i+j] == sub[j]:
j++
else:
i++
j = 0
return i if j == sub_len else None
if __name__ == '__main__':
string = 'spamspam'
sub = 'msp'
res_for = naive_search_for(string, sub)
res_while = naive_search_while(string, sub)
assert res_for == res_while