python recursive function that prints from 0 to n?

前端 未结 5 1126
陌清茗
陌清茗 2020-12-01 21:36

I am trying to write a recursive function that prints from 0 to n, but I have no idea how to do it. I accidentally made one that prints from

相关标签:
5条回答
  • 2020-12-01 22:07

    You're about 99% there.

    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?

    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.

    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)
    
    0 讨论(0)
  • 2020-12-01 22:11

    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 print statement must be placed after the 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 return None (Python's default return value)

    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:

    @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
    
    0 讨论(0)
  • 2020-12-01 22:11

    you can try this method as well:

    def recursion_print(n):
        if n==0:
          print(n)
        else:
          recursion_print(n-1)
          print(n)
    
    
    recursion_print(9)
    
    0 讨论(0)
  • 2020-12-01 22:14

    You can replace the 0 and the n, and the + with a - to make your recursive countdown function to a recursive countup:

    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 article about the difference between a linear and iterative recursion, if used with the @tail_call_optimized decorator.

    0 讨论(0)
  • 2020-12-01 22:20

    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)) 
    
    0 讨论(0)
提交回复
热议问题