I am working on an assignment in python. The goal is to make a function with two parameters, one for a string and the other for a pattern. If the pattern appears in the string, the program must return true, or false if otherwise. Also, the pattern can accept *'s to represent any number of characters can appear in between and the pattern is still good.
ex.
matchPat(a*t*r. anteaters)
True
matchPat(a*t*r. albatross)
Ture
matchPat(a*t*r. albatross)
False
I have the function set to look through the string one at a time for a match with the first character of the pattern. If true it dumps that character and letters in the string it looked for.
It goes through good enough, but once I get to the end I get an index error since I am usually passes pattern in as "" which isnt allowed. Problem is when I block that instance all I get for an output is None and not true which is what I need.
I put some print statements up and noticed when I get to the end of pattern and the end of the string, I get a random burp of previous values, so I am guessing something in my recursion is off. Any pointers pros? Oh, and no uses of loops
def matchPat(CH, string):
print(CH)
print(string)
input("")
if not CH:
return True
elif CH[0] == string[0]:
return matchPat(CH[1:], string[1:])
elif CH[1] == string[0] or matchPat(CH, string[1:]):
matchPat(CH[1:], string)
print(CH)
print(string)
print(matchPat("a*t*r","anteater"))