I'm trying to manually code the python string in
function for an assignment. Using this code, where s
is the string and t
is the substring I am trying to find:
def test(s, t):
stidx = 0
while stidx < len(s):
idx = 0
for i in s[stidx:]:
if idx < len(t):
if t[idx] == i:
idx += 1
continue
else:
break
if idx == len(t):
return True
stidx += 1
return False
The above code works, except when I am checking a substring at the very end of the word (e.g. s = 'happy'
and t = 'py'
). If I add an arbitrary character to the end of s
, it works. Why is this?