简体   繁体   English

Python 递归函数如何用于 tri_recursion 函数?

[英]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.我是 Python 新手,我发现下面的递归程序很难理解。 While debugging the program I could find that it goes through recursion and the value of k decrements -1 every time we recurse.在调试程序时,我发现它经历了递归,每次递归时k的值都会递减 -1。 At one point k is -1 and the compiler moves to the else part and returns 0.在某一时刻k为 -1,编译器移至else部分并返回 0。

Finally, the k value turns out to be 1, how does this happen?最后, k值竟然是 1,这是怎么发生的?

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  

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)的结果,我们必须得到tri_recursion(5)的结果,按照这个逻辑,问题tri_recursion(5)为:

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:现在注意 0 不大于 0,所以程序移动到 else 子句的主体:

else:
    result = 0
...

Which means tri_recursion(0) = 0 .这意味着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. k永远不等于-1 ,事实上这是不可能的。
  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).编译器在执行期间不做任何事情( JIT是另一回事)。 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.最好从过程语言中的控制流/执行顺序、函数式编程中的等式和逻辑编程中的关系方面进行思考。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM