I tried to write a string matching program using a recursive function. The function first creates an empty list called tuple1 to add the points. Then it returns the list. However, when I try to use this function twice, the function adds the points to the list created in the previous function. Why doesn't the function use the default value tuple1 = [] in the second call? Any ideas??
The output of the program:
[0, 3, 5, 9, 11, 12, 15, 19]
[0, 3, 5, 9, 11, 12, 15, 19, 0, 5, 15]
btw this is an assignment from OpenCourseWare shared by MIT.
def subStringMatchExact(target, key, counter=0, tuple1=[]):
if len(target) < len(key):
return tuple1
else:
counter += 1
if target[:len(key)] == key:
tuple1.append(counter-1)
return subStringMatchExact(target[1:], key, counter, tuple1)
print(subStringMatchExact("atgacatgcacaagtatgcat", "a"))
print(subStringMatchExact("atgacatgcacaagtatgcat", "atg"))