Increasing + Decreasing in One Function

Print numbers increasing then decreasing recursively.

AdvancedPhase 3 - Level 3: Pattern RecursionExample 8 of 10
increasing-decreasing-in-one-function.py
1def print_inc_dec(n, current=1):
2 # Base case
3 if current > n:
4 return
5
6 # Print increasing
7 print(current, end=" ")
8 # Recurse
9 print_inc_dec(n, current + 1)
10 # Print decreasing (after recursion)
11 if current < n:
12 print(current, end=" ")
13
14# Test
15n = int(input("Enter n: "))
16print_inc_dec(n)
17print()

Output

Enter n: 5
1 2 3 4 5 4 3 2 1

What's going on

Print before recursion (increasing), after (decreasing).

Key Concepts:

Print before recursion: increasing order
Print after recursion: decreasing order
Creates symmetric pattern