[英]Nested recursive function in python
I tried to implement a nested recursive function in python it is giving an error "RuntimeError: maximum recursion depth exceeded" you can see the function in the following code. 我试图在python中实现嵌套递归函数,它给出了错误“ RuntimeError:超出最大递归深度”,您可以在以下代码中看到该函数。 your help regarding the question is appreciated.
感谢您对问题的帮助。
n=10
def test(n):
if n<=0:
return 1
else:
return test(test(n-1)+1)
print test(n)
One very important part of recursion is that with every recursive call you have to get closer to your anchor case, which in this case is: 递归的一个非常重要的部分是,每次递归调用都必须更接近锚点,在这种情况下,它是:
if n<=0:
return 1
However, with your code you are not getting close to this case. 但是,使用您的代码,您将无法接近这种情况。 The problem is this line:
问题是这一行:
return test(test(n-1)+1)
Since test returns 1
when it reaches the end case and you add 1 to this result, let's see what happens when we call test(2)
: 由于test到达末尾时返回
1
,并且您向该结果加1,因此让我们看看调用test(2)
会发生什么:
The anchor case isn't executed and you go straight into the else
. 锚例没有被执行,您直接进入
else
。 The you return 你回来
test(test(1)+1)
since 2-1=1
. 因为
2-1=1
。 Your inner test call is also going to go to the else
case and call: 您的内部测试调用也将转到
else
案例并调用:
test(test(0)+1)
Here your inner test call returns 1 (it has already reached the end case) which means that essentially in this line you are calling 在这里,您的内部测试调用返回1(它已经达到了最终情况),这意味着您实际上在此行中调用
test(2) //test(1+1)
again. 再次。 Here you can see that your recursion is never ending, hence your
maximum recursion depth exceeded error
. 在这里,您可以看到递归永远不会结束,因此
maximum recursion depth exceeded error
。
Just to clarify how you could make this code (which is obviously just an example) work: 只是为了阐明如何使此代码(显然只是示例)起作用:
def test(n):
if n <= 0:
return 1
else:
return test(test(n-1)-1) //notice the -1 instead of +1
Follow up question: 后续问题:
Why does changing the recursive call to test(test(n-2)) also result in infinite recursion? 为什么将递归调用更改为test(test(n-2))也会导致无限递归?
Well this is basically because of the same reason that I pointed out right at the beginning. 嗯,这基本上是因为我一开始就指出了同样的原因。 You need to get closer to your anchor case.
您需要靠近锚盒。 While you can reach the case of
n<=0
inside the nested recursive call test(n-2)
you can certainly not reach it inside the outer function call. 虽然您可以在嵌套递归调用
test(n-2)
达到n<=0
的情况,但您肯定不能在外部函数调用中达到n<=0
的情况。
Note that your function returns 1 when it reaches it's end case, so even if test(n-2)
doesn't cause any more recursions it returns 1 (not 0), which means you end up with 请注意,您的函数在到达末尾时将返回1,因此,即使
test(n-2)
不再引起任何递归,它也会返回1(而不是0),这意味着您最终会获得
test(1)
which is again going to cause an infinite loop (since you can not get n to be <= 0
for this outer function call). 这将再次导致无限循环(因为对于此外部函数调用,您不能使n
<= 0
等于<= 0
)。
You have multiple options to make the recursion work for this case: By changing your return value or by changing your anchor case. 您有多种选择可以使这种情况的递归工作:通过更改返回值或通过更改锚点大小写。 So changing the if statement to either of these will prevent an infinite recursion:
因此,将if语句更改为以下任意一个将防止无限递归:
if n <= 0:
return 0
or 要么
if n <= 1:
return 1 //you could also return any other value <= 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.