Skip to content

Instantly share code, notes, and snippets.

@rishi93
Created September 15, 2015 05:51
Show Gist options
  • Save rishi93/2fe8de93713c4edb0aea to your computer and use it in GitHub Desktop.
Save rishi93/2fe8de93713c4edb0aea to your computer and use it in GitHub Desktop.
Naive Pattern Searching
def search(string,pattern):
string_len = len(string)
pattern_len = len(pattern)
for i in range(0,string_len - pattern_len + 1):
for j in range(0,pattern_len):
if string[i+j] != pattern[j]:
break
if j == pattern_len - 1:
print("Pattern found at index",i)
print(search("thepatternishere","pattern"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment