I'm looking for feedback on the actual logic of this simple Python 2.7 script. The automated grader passed it, but I expect it could be make more 'pythonic'. It was written for homework, and some of the constraints included basic looping constructs only and no function declarations. This differs from a simple String.find()
because it must count substrings with common characters. For example, "bobob" must match "bob" twice.
#! /usr/bin/python
s = 'azcbobobegghakl' #2 bobs
s = 'auoeunbobnoeubobob,ntonethbobobo' #5 bobs
seek = 'bob'
matches = 0
for i in range(0,len(s)):
if s[i] == seek[0]: #if any input char matches seek string
if abs(i-len(s)) < len(seek): #checks not closer to end of input than seek
break
match_maybe = True
match = False
seek_index = 0
while (match_maybe): #look ahead to see if subsequent chars match seek
seek_index += 1
if seek_index == len(seek): #successful match each char of seek
match = True
break
if s[i+seek_index] != seek[seek_index]: #does not match
match_maybe = False
break
if match:
matches += 1
print 'Number of times bob occurs is: ' + str(matches)