Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a Python recursive function works for tri_recursion function?

I'm new to Python, I found the below recursive program being tough to follow. While debugging the program I could find that it goes through recursion and the value of k decrements -1 every time we recurse. At one point k is -1 and the compiler moves to the else part and returns 0.

Finally, the k value turns out to be 1, how does this happen?

def tri_recursion(k):
  if(k>0):
    result = k+tri_recursion(k-1)
    print(result)
  else:
    result = 0
  return result

print("\n\nRecursion Example Results")
tri_recursion(6)

And the output:

Recursion Example Results  
1  
3  
6  
10  
15  
21  
like image 675
Balaji Avatar asked Sep 30 '18 14:09

Balaji


2 Answers

Try tracing the function with a pencil and paper. In this case, the print statement insde the function may be a bit misleading.

Consider this part of the program,

if(k>0):
    result = k+tri_recursion(k-1)
...

From here,

tri_recursion(6) = 6 + tri_recursion(5)

So to get the result for tri_recursion(6) we must get the result of tri_recursion(5) Following this logic, the problem reduces to:

tri_recursion(6) 
 = 6 + tri_recursion(5) 
 = 6 + 5 + tri_recursion(4)
 = 6 + 5 + 4 + tri_recursion(3)
 = 6 + 5 + 4 + 3 + tri_recursion(2)
 = 6 + 5 + 4 + 3 + 2 + tri_recursion(1)
 = 6 + 5 + 4 + 3 + 2 + 1 + tri_recursion(0)

Now notice that 0 is not greater than 0 so the program moves to the body of the else clause:

else:
    result = 0
...

Which means tri_recursion(0) = 0. Therefore:

tri_recursion(6) 
= 6 + 5 + 4 + 3 + 2 + 1 + tri_recursion(0)
= 6 + 5 + 4 + 3 + 2 + 1 + 0
= 21

Points to note

  1. In running this program.k is never equal to -1, infact it is impossible.
  2. It is misleading to think of control flow in terms of "the compiler moving across a program". The compiler does't do anything during execution (JIT is a different matter). It is better to think in terms of control flow / order of execution in procedual languages, equationally in functional programming and relations in logic programming.
like image 99
Xero Smith Avatar answered Oct 22 '22 03:10

Xero Smith


if you debug the code like this

def tri_recursion(k):
    if(k > 0):
        print('\t'*k,'start loop k',k)
        holder = tri_recursion(k - 1)
        result = k + holder
        print('\t'*k,'i am k(', k,')+previous result(', holder,')=',result)
    else:
        result = 0
        print('i reached when k =', k)
    print('\t'*k,'end loop', k)
    return result

print("\n\nRecursion Example Results")
tri_recursion(6)

you will see the output like

Recursion Example Results
                         start loop k 6
                     start loop k 5
                 start loop k 4
             start loop k 3
         start loop k 2
     start loop k 1
i reached when k = 0
 end loop 0
     i am k( 1 )+previous result( 0 )= 1
     end loop 1
         i am k( 2 )+previous result( 1 )= 3
         end loop 2
             i am k( 3 )+previous result( 3 )= 6
             end loop 3
                 i am k( 4 )+previous result( 6 )= 10
                 end loop 4
                     i am k( 5 )+previous result( 10 )= 15
                     end loop 5
                         i am k( 6 )+previous result( 15 )= 21
                         end loop 6
21
like image 22
Datalicious Avatar answered Oct 22 '22 04:10

Datalicious