Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions strings/naive_string_search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
https://en.wikipedia.org/wiki/String-searching_algorithm#Na%C3%AFve_string_search

this algorithm tries to find the pattern from every position of
the mainString if pattern is found from position i it add it to
the answer and does the same for position i+1
Expand All @@ -9,7 +11,20 @@
"""


def naivePatternSearch(mainString, pattern):
def naivePatternSearch(mainString: str, pattern: str) -> list:
"""
>>> naivePatternSearch("ABAAABCDBBABCDDEBCABC", "ABC")
[4, 10, 18]

>>> naivePatternSearch("", "ABC")
[]

>>> naivePatternSearch("TEST", "TEST")
[0]

>>> naivePatternSearch("ABCDEGFTEST", "TEST")
[7]
"""
patLen = len(pattern)
strLen = len(mainString)
position = []
Expand All @@ -24,9 +39,5 @@ def naivePatternSearch(mainString, pattern):
return position


mainString = "ABAAABCDBBABCDDEBCABC"
pattern = "ABC"
position = naivePatternSearch(mainString, pattern)
print("Pattern found in position ")
for x in position:
print(x)
if __name__ == "__main__":
assert naivePatternSearch("ABCDEFG", "DE") == [3]