I'm trying to write a recursive function that writes how many occurrences of several strings in one single text there are. The reason why I'm using recursion here is because I'm trying to solve a more complex problem this way, where it feels like a simple loop would not be as efficient or would look messy.
So we have a string cat = 'cat kat not kat cat kat c not c' and a list of strings we need to look for: search = ['cat', 'kat']. I'm trying to return a list that gives the number of occurrences of each word: result = [2, 3]. Here's my code:
cat = 'cat kat not kat cat kat c not c'
search = ['cat', 'kat']
result = []
def cat_main(cat, search, result):
for el in search:
count1 = cat.count(el)
return look_for_cat(el, search, count1, result)
def look_for_cat(el, search, count1, result):
if count1 == 0:
return result
else:
result.append(el)
count1 -= 1
look_for_cat(el, search, count1, result)
When I call the cat_main function, it doesn't return anything. When I try to print things inside of both functions separately, it seems to work, so I'm probably making some dumb mistake but I have no idea why it doesn't return anything.