问题描述
我正在研究Think Python,并且已经达到了递归,这对我来说很难理解。
I'm going through Think Python and I've reached recursion which is turning to be a great pain to understand for me.
,编号5,向我展示了这段代码:
There's this exercise, number 5, that shows me this piece of code:
def draw(t, length, n):
if n == 0:
return
angle = 50 #sets angle
fd(t, length*n) #make a turtle() "t" go forward length*n pixels while drawing
lt(t, angle) #makes turtle "t" turn left on itself "angle" (50) degrees
draw(t, length, n-1) #1st call
rt(t, 2*angle) #makes turtle "t" turn right "2*angle" (100) degrees
draw(t, length, n-1) #2nd call
lt(t, angle) #makes turtle "t" turn left "angle" (50) degrees
bk(t, length*n) #makes turtle "t" go backwards length*n pixels
并请我思考它做什么,然后运行它。我运行了它,但很难理解为什么会这样做。
递归的情况要复杂得多,因为本书中介绍了有关该设备的书籍,我无法理解。
让我们以n为例2,以了解此问题的一个简单实例:
我能弄清楚的是,代码会连续调用直到n = 0为止的第一个调用,然后返回控制到n = 1,并使剩下的代码行从调用1到调用2。它使第二次调用的n = 0,并返回,但是我不明白该函数的哪个实例返回程序的控制权。
我很高兴有人能指出正确的方向,例如我自己如何思考这种递归代码,我该如何利用它(当for语句不能完全实现时)和模式化其工作方式的方法(例如,使用某种图表?)。
我有这个:
And asks me to think of what it does and then run it. I ran it and I had trouble understanding why it does what it does.It is a much more complex case of recursion that the ones the books presented for explaining this device and I cannot understand it.Lets make n for example 2 in order to understand a simple instance of this problem:What I can make out is that the code calls itself until n=0 up to the first call consecutively, then returns the control to n=1 and makes the remaining lines of code from call 1 to call 2. It makes the second call with n=0 and returns but I cannot understand what instance of the function it returns the control of the program.I'd be glad if someone could point me in the right direction as how to think this kind of recursive code by myself, how can I make use of it (when a for statement wouldn't quite make it) and a way to schematize the way it works (with some sort of diagram, for example?).I have this:
function called with n=2
function called with n=1
function called with n=0; returns to:
function called with n=1 makes the 2nd call to the function with n=0
function called with n=0; returns to where?
??????
如您所见,对于n = 7的函数调用来说,这是不切实际的。
As you can see it is quite impractical for, let's say, a function call with n = 7.
推荐答案
函数总是直接返回其调用者。您的递归函数会做一些工作,调用自身,然后在调用返回时做其他工作,然后在另一时间处调用自己,然后在返回时进行最后的工作,然后函数结束并自行执行返回:
A function always returns directly to its caller. Your recursive function does some work, calls itself, then when the call returns, does some more work, then calls itself another time, then when that returns, does some final work, before the function ends and itself returns:
work
call
work
call
work
return
也就是说,除非 n == 0
为真,则该函数立即中断。每个递归调用都从 n
中减去1,只要 n
开始是正数,您的递归就在某个时刻结束
That is, unless n == 0
is true, then the function immediately returtns. Every recursive call subtracts 1 from n
so as long as n
was positive to start with, your recursion ends at some point.
递归只是再次运行相同功能的行为,因此您可以在上面替换每个调用
执行相同作业的图表。让我们这样做一次:
Recursion is simply the act of running the same function again, so you can substitute each call
in the above 'diagram' with the same jobs being executed. Lets do this once:
n = 2
work
n = 1
work
call
work
call
work
work
n = 1
work
call
work
call
work
return
work
return
I'如果我们以 n = 2
开始,则为 n
添加值。当然,一旦下降到 n = 0
,该调用将立即返回;我们也可以填写:
I've added in the value for n
, provided we start with n = 2
. Of course, once you get down to n = 0
, the call immediately returns; we can fill that in too:
n = 2
work
n = 1
work
n = 0
return
work
n = 0
return
work
return
work
n = 1
work
n = 0
return
work
n = 0
return
work
return
work
return
现在我们有了递归函数的完整调用树,其中 n = 2
。对于 n
的较高值,只需缩进并填写 work-call-work-call-work-return
在上一行有 call
行的任何地方排成一行,您可以为任何递归函数构建完整的调用树。
Now we have the full call tree for your recursive function, for n = 2
. For higher values of n
, just keep indenting and filling in the work - call - work - call - work - return
lines anywhere there is a call
line for the previous level, and you can build a full call tree for any recursive function.
这篇关于了解Python 2中的递归(思考Python,练习5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!