Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive python script

Tags:

python

found = 0
def new(string):
    global found

    if found > len(string):
        return 0 

    fish = string.find('x',found,len(string))
    found = fish + 1

    return new(string) + 1


text = 'onxonxoinxoinoxn'
final_text = text + 'x'
print new(final_text)

So I'm new to recursion and i know there is a much easier way to do this but can someone explain how to fix this.this is basically a recursive function to find the total number of times a letter 'x' can be found in the variable 'text'.

This is my error:
4
7
11
16
18
0
4
7
Traceback (most recent call last):
11
16
  File "/Users/Charana/Documents/Projects/untitled/Main.py", line 18,      

in new(final_text) RuntimeError: maximum recursion depth exceeded

so it works but it continues to loop.how do i make it stop thankyou in advance

like image 338
Charana Avatar asked Oct 15 '25 16:10

Charana


1 Answers

found > len(string)

This condition will never be true, because str.find will always return a result < len(s).

The correct return value to check for when there was no result is -1. But you need to be careful with the increment, since that will change the invalid result -1 to 0 continuing the loop. So you should reorder your logic a bit:

def new(string):
    global found

    fish = string.find('x',found,len(string))
    if fish < 0:
        return 0 

    found = fish + 1
    return new(string) + 1

Note that using global variables for such a function, especially for recursive functions, is a bad idea. You don’t have full control over it, and instead, you also need to make sure that you reset its value when you call the function. Instead, you should keep all the information inside, and pass it around to the recursive calls if necessary. You could change your function like this:

def new (string, found = 0):
    fish = string.find('x', found)
    if fish < 0:
        return 0
    return new(string, fish + 1) + 1

This uses default parameter values to make sure that found starts with 0. And for the recursive call, it just passes the new found value, so the next function can start there.

Finally note, that you should try to use descriptive names for your functions and variables. The function is supposed to count the number of occurrences of 'x', so maybe count_x would be better. Also, the variable found in that context conveys a meaning that it contains the number of occurrences of x that you have already found; but instead, it’s the start offset from which to continue the search; and fish is just bad, as it’s just the index of the next 'x':

def count_x (string, offset = 0):
    index = string.find('x', offset)
    if index < 0:
        return 0
    return count_x(string, index + 1) + 1

Finally, just in case you don’t know, there is also a built-in function str.count which does the same thing :)

like image 105
poke Avatar answered Oct 17 '25 05:10

poke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!