3

I am supposed to write a recursive function counting(5) that prints 5 4 3 2 1 0 1 2 3 4 5.
I have made two functions down below that do half part each but I need them to be put together.

def countdown(n):
    if n == 0:
        print 0
    else:
        print n,
        countdown(n-1)

def countup(n):
    if n >= 1:
        countup(n - 1)
        print n,
Anil_M
  • 10,893
  • 6
  • 47
  • 74
Therapist
  • 39
  • 1
  • 1
  • 2

3 Answers3

7

I suppose the trick is to understand the recursion point does not end execution:

def count_down_up(n):
    if not n:
        print n  # prints 0 and terminates recursion
        return
    print n  # print down 5, 4, 3, 2, 1
    count_down_up(n-1)  # recursion point
    print n  # prints up 1, 2, 3, 4, 5

You can see each step print n, <RECURSION>, n, which unfolds to:

5, <count_up_down 4>, 5
5, 4, <count_up_down 3>, 4, 5
# ...
5 ,4, 3, 2, 1, <count_up_down 0>, 1, 2, 3, 4, 5   # recursion stops ...
5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • 2
    Super sleek answer + 1. I think `not n` is also one of the most important line as when `n` reaches `0` , `not n` is TRUE and then inversion kicks in. – Anil_M Sep 13 '17 at 16:00
  • 1
    As a beginner this took me way too long to understand.. The last print() statement was confusing as i thought the recursive function would be called until reaching 0 and end the whole thing before even getting there. But after understanding: 'I suppose the trick is to understand the recursion point does not end execution' it clicked. It is still a bit confusing but this was a big lesson to me, thanks! – Lew Pérez Apr 20 '22 at 20:43
3

@Reut_Sharabani solution is fine but I think this is simpler to read :

def countdown(N,n):
    if abs(n) > N:
        return
    else:
        print(abs(n))
        countdown(N,n-1)

Call like this :

countdown(5,5)
Abdennacer Lachiheb
  • 4,388
  • 7
  • 30
  • 61
0

One way to do it is by keeping track of 2 lists during the recursion and then stiching them together at return at the end.

def countdown_up(n, i=0, down=[], up=[] ):
    if n >i:
        return countdown_up(n-1, i, down+[n], [n]+up)
    else:
        # stich the 2 lists together with the low point [i]
        return down+[i]+up

# by default it counts down to 0. 
>>>countdown_up(5)
[5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5]
# you can run with any arbitrary high and low numbers. 
>>>countdown_up(10, 3)
[10, 9, 8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9, 10]    

To have the function print instead of return list we just need to change 1 line.

def countdown_up(n, i=0, down=[], up=[] ):
    if n >i:
        return countdown_up(n-1, i, down+[n], [n]+up)
    else:
        print(" ".join("%s "%x for x in down+[i]+up))


>>>countdown_up(5)
5  4  3  2  1  0  1  2  3  4  5 
>>>countdown_up(10,3)
10  9  8  7  6  5  4  3  4  5  6  7  8  9  10 
PabTorre
  • 2,878
  • 21
  • 30