-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
148 lines (104 loc) · 3.13 KB
/
Copy pathsearch.py
File metadata and controls
148 lines (104 loc) · 3.13 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import re
"""
This is the "search" module.
The search module supplies function, search00x(word, sentence) whose responsibility is
to return an list of indexes of every accurence on an substring in a string.
For example,
>>> search001('aa', 'makkaanada')
[4]
"""
def search001(word, sentence):
"""
Return tha list of index of a substring found in string
>>> search001('aa', 'makkaanada')
[4]
>>> search001('ad', 'makkaanada')
[7]
>>> search001('an', 'makkaanada')
[5]
"""
return [sentence.find(word)]
def search002(word, sentence):
"""
Return tha list of indexs of a substring found in string
>>> search002('aa', 'makkaanada')
[4]
>>> search002('ad', 'makkaanada')
[7]
>>> search002('an', 'makkaanada')
[5]
"""
return [re.search(word, sentence).span()[0]]
def search003(word: str, sentence: str) -> list:
"""
Return tha list of indexs of a substring found in string
>>> search003('aa', 'maakkaanaakka')
[1, 5, 8]
>>> search003('kk', 'maakkaanaakka')
[3, 10]
>>> search003('ak', 'maakkaanaakka')
[2, 9]
"""
return [match.start() for match in re.finditer(word, sentence)]
"""
plot twist the function has to be very fast
- using str native functions in not an option
the response may be a generator
"""
def search004(word: str, sentence: str):
"""
Return tha list of indexes of a substring found in string
>>> search004('aa', 'maakkaanaakka')
[1, 5, 8]
>>> search004('kk', 'maakkaanaakka')
[3, 10]
>>> search004('ak', 'maakkaanaakka')
[2, 9]
"""
response = []
word_len = len(word)
for i in range(len(sentence) - word_len):
if (word == sentence[i:i+word_len]):
response.append(i)
return response
def search005(word: str, sentence: str):
"""
Return tha list of indexes of a substring found in string
>>> list(search005('aa', 'maakkaanaakka'))
[1, 5, 8]
>>> list(search005('kk', 'maakkaanaakka'))
[3, 10]
>>> list(search005('ak', 'maakkaanaakka'))
[2, 9]
"""
word_len = len(word)
for i in range(len(sentence) - word_len):
if (word == sentence[i:i + word_len]):
yield i
def search006(word: str, sentence: str):
"""
Return a generator of indexs of a substring found in string
>>> list(search006('aa', 'maakkaanaakka'))
[1, 5, 8]
>>> list(search006('kk', 'maakkaanaakka'))
[3, 10]
>>> list(search006('ak', 'maakkaanaakka'))
[2, 9]
>>> list(search006('au', 'maakkaanaakka'))
[]
>>> list(search006('ana', 'maakkaanaakka'))
[6]
>>> list(search006('ana', 'maannakkaanaakka'))
[9]
"""
word_len = len(word)
for i in range(len(sentence) - word_len):
for j in range(word_len):
if (sentence[i+j] == word[j]):
if (j == word_len-1):
yield i
else:
break
if __name__ == "__main__":
import doctest
doctest.testmod()