I’m currently stuck on trying to make a naive algorithm which given a piece of a pattern e.g aabba
search for it in a text e.g abbbbaababaabbaaabbaa one letter at a time. It will compare a with the text if that is right then compares the next letter and if that’s wrong the whole pattern will shift one and compare a with b etc
we were give code example
print "Input text: ",
text = raw_input()
print "Input pattern: ",
pattern = raw_input()
index = text.find(pattern)
while index > -1:
print index
index = text.find(pattern, index+1)
but the find() function in python is too fast(I need a non optimized sort of algorithm, using while
and for loops statements I guess).
Any help appreciated,
Thanks
I guess here’s what you need, the following code does character-by-character comparison. You may also replace the calls to
findby iterations overtextwhich includes checks whether the first character oftextmatches the first character ofpattern: