Home > other >  Doubts on how recursion works in Python
Doubts on how recursion works in Python

Time:10-18

I'm new to Python. I'm trying to do a recursive function in which i put a list of numbers as an input. and it should give as an output the number(counter) of sublists that have subtotal as the result of the numbers inside of them. I tried building a recursive function, and when I arrive at what I guess should be the base case, I have the right result (when debugging, at some point) in counter. But at the end of the function the value my counter is just 0. I know it's because of exiting calls, but haven't really understood how this works. What am I doing wrong with my code and how can I correct it?

  def count (lista,i,j,accumulatore,counter):
        if i > len(lista)-1: return counter
        else:
            if accumulatore < subtotal :
                if j > len(lista)-1:
                    i =1
                    count (lista,i,i,0,counter)
                else:
                    accumulatore  = lista[j]
                    j =1
                    count(lista,i,j,accumulatore,counter)
            elif accumulatore == subtotal:
                counter =1
                if j > len(lista)-1:
                    i =1
                    count(lista,i,i,0,counter)
                else:
                    accumulatore =lista[j]
                    j =1
                    count(lista,i,j,accumulatore,counter)
            else: #accumulator > subtotal
                i =1
                count(lista,i,i,0,counter)

CodePudding user response:

Your basic problem are these calls in your code:

count (lista,i,i,0,counter)

Note that you're calling count but you're not returning the value anywhere except the base case. So, when your else clause ends, the function returns a default None.

It's a little unclear what you expect your inputs and outputs to be, but you can almost certainly simplify this function. Remember, for recursion the basic idea is to reduce the size of your problem until you hit some base case, and then use the response of the next-smallest-problem output to construct your own output.

For that reason, accumulators aren't usually part of a properly deployed recursive solution.

CodePudding user response:

I corrected it and it's working! subtotal has to be defined outside in this case

  def conta (lista,i,j,accumulatore,contatore):
        if i > len(lista)-1: return contatore
        else:
            if accumulatore < subtotal :
                if j > len(lista)-1:
                    return conta (lista,i 1,i 1,0,contatore)
                else:
                    return conta(lista,i,j 1,accumulatore lista[j],contatore)
            elif accumulatore == subtotal:
                if j > len(lista)-1:
                    return conta(lista,i 1,i 1,0,contatore 1)
                else:
                    return conta(lista,i,j 1,accumulatore lista[j],contatore 1)
            else: #caso in cui accumulatore > subtotale
                return conta(lista,i 1,i 1,0,contatore)

Edit : I'm running across recursion limit problems and am translating it to an iterative fun-ction

  • Related