从0打印到n的python递归函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17127355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python recursive function that prints from 0 to n?
提问by user2489526
I am trying to write a recursive function that prints from 0to n, but I have no idea how to do it. I accidentally made one that prints from nto 0though:
我正在尝试编写一个从0to打印的递归函数n,但我不知道该怎么做。我不小心做了一个从n到打印的0:
def countdown(n):
print(n)
if n == 0:
return 0
return countdown(n - 1)
I don't know if that helps or not, maybe I can change something in the code to make it go from 0to n?
我不知道这是否有帮助,也许我可以更改代码中的某些内容以使其从0变为n?
采纳答案by óscar López
You almost got it! here's a fixed, simplified version:
你几乎明白了!这是一个固定的简化版本:
def countup(n):
if n >= 0:
countup(n - 1)
print(n)
Notice that:
请注意:
- You don't have to return anything from a recursive function that only prints values
- For printing in ascending order, the
printstatement must be placed afterthe recursive call - The recursion exits when
n < 0, given that we're only printing, there's nothing left to be done afterwards and it's ok to returnNone(Python's default return value)
- 您不必从仅打印值的递归函数中返回任何内容
- 对于升序打印时,
print声明必须放在后递归调用 - 递归退出 when
n < 0,考虑到我们只是打印,之后没有什么可做的并且可以返回None(Python的默认返回值)
UPDATE
更新
It seems that writing a tail recursive solution is all the rage around here :) oh well, here's my shot at it, a simplified and tail-recursive version of @AndyHayden's idea - using the tail call optimization decorator recipe:
似乎编写尾递归解决方案在这里风靡一时:) 哦,这是我的尝试,@AndyHayden 想法的简化尾递归版本 - 使用尾调用优化装饰器配方:
@tail_call_optimized
def countup(N, n=0):
print(n)
if n < N:
countup(N, n + 1)
Either way, it works as expected:
无论哪种方式,它都按预期工作:
countup(5)
=> 0
1
2
3
4
5
回答by Andy Hayden
You can replace the 0 and the n, and the + with a - to make your recursive countdown function to a recursive countup:
您可以将 0 和 n 以及 + 替换为 - 以使您的递归倒计时功能成为递归计数:
def countup(N, n=0):
print(n)
if n == N:
return
return countup(N, n + 1)
And call it as follows:
并按如下方式调用它:
countup(3)
@JFSebastian points out this algorithm has the benefit of being O(1) rather than O(n), as discussed in this excellent articleabout the difference between a linear and iterative recursion, if used with the @tail_call_optimizeddecorator.
@JFSebastian 指出这个算法的好处是 O(1) 而不是 O(n),正如这篇关于线性和迭代递归之间区别的优秀文章中所讨论的,如果与@tail_call_optimized装饰器一起使用。
回答by Makoto
You're about 99% there.
你在那里大约有 99%。
Think of your base case and your recursive step - when you hit 0, what do you want to do? When you're still working your way down from n, what do you want to happen?
想想你的基本情况和你的递归步骤——当你达到 0 时,你想做什么?当你还在努力工作的时候n,你想发生什么?
If you reverse the order in which you print the value, you'll reach your desired result.
如果您颠倒打印值的顺序,您将获得所需的结果。
def countdown(n):
if n != 0:
countdown(n-1)
print(n)
The reason this works is that recursive calls go on the call stack. As you push calls onto the stack, while your end case isn't met, you'll keep adding more calls until you reach your base case of n == 0, and then you'll exclusively start printing the values.
这样做的原因是递归调用在调用堆栈上进行。当您将调用压入堆栈时,虽然您的最终情况未得到满足,但您将继续添加更多调用,直到达到 的基本情况n == 0,然后您将专门开始打印值。
The other calls will then fall through to the print statement, as their execution has returned to the line after the conditional.
然后其他调用将落入打印语句,因为它们的执行已返回到条件之后的行。
So, the call stack looks something like this:
所以,调用堆栈看起来像这样:
countdown(5)
countdown(4)
countdown(3)
countdown(2)
countdown(1)
countdown(0)
print(0)
print(1)
print(2)
print(3)
print(4)
print(5)
回答by Baishakhi Dasgupta
You can do this
你可以这样做
def fun(num,n):
if num<n:
print(num)
fun(num+1,n)
n=int(input())
print(fun(0,n+1))

