1

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.

0

4 Answers 4

2

A good way to debug your code and step through it is PythonTutor.

But I would solve your problem with a list comprehension like so:

>>> [cat.split().count(word) for word in search]
[2, 3]
2
  • 1
    Note that in its current form, if you added 'c' to search then it would return a result of 4 (as there are 4 instances of the character 'c' within the cat string).
    – Neil Studd
    Commented Apr 26, 2021 at 14:33
  • 1
    @NeilStudd Good catch. I updated the code.
    – BioGeek
    Commented Apr 26, 2021 at 14:34
2

I highly suggest BioGeek solution, however, if you wanted to fix yours here it is...

cat = 'cat kat not kat cat kat c not c'
search = ['cat', 'kat']
result = []

def cat_main(cat, search, result):
    result_list = []
    for el in search:
        count1 = cat.count(el)
        temp_result = look_for_cat(el, search, count1, result)
        result_list.append(temp_result)
    return result_list

def look_for_cat(el, search, count1, result):
    if count1 != 0:
        result.append(el)
        count1 -= 1
        look_for_cat(el, search, count1, result)
    return result

print(cat_main(cat, search, result))
1

If you want to keep it readable, then you can achieve this without recursion in the following manner:

cat = 'cat kat not kat cat kat c not c'
search = ['cat', 'kat']
result = []

for word in search:
     result.append(cat.split().count(word))

print(result) # [2, 3]

The split() ensures it tokenizes cat into individual words, so that a search for c would return 2 rather than 4.

If efficiency is more important for you, refer to the marked answer of this question: Finding occurrences of a word in a string in python 3

0

This should work(if you want only through reccursion)

cat = 'cat kat not kat cat kat c not c'
search = ['cat', 'kat']
result = []


def cat_main():
    for el in search:
        count1 = cat.count(el)
        print(count1)
        look_for_cat(el, count1) #if you return here it will exist out of function 
                                 #doesnt continue for further elements in search list


def look_for_cat(el, count1):
    if count1 == 0:
        return
    else:
        result.append(count1) #no need to append words according to your question, just their count

cat_main()
print(result)

Not the answer you're looking for? Browse other questions tagged or ask your own question.