3. Print Count
In [6]:
Copied!
def print_count_descending(n):
"""
Example of tail recursion
"""
# 1. Base case
if n == 0:
return
# 2. Processing
print(n)
# 3. Recurrence relation
print_count_descending(n - 1)
def print_count_descending(n):
"""
Example of tail recursion
"""
# 1. Base case
if n == 0:
return
# 2. Processing
print(n)
# 3. Recurrence relation
print_count_descending(n - 1)
In [7]:
Copied!
print_count_descending(5)
print_count_descending(5)
5 4 3 2 1
In [8]:
Copied!
def print_count_ascending(n):
"""
Example of head recursion
"""
# 1. Base case
if n == 0:
return
# 2. Recurrence relation
print_count_ascending(n - 1)
# 3. Processing
print(n)
def print_count_ascending(n):
"""
Example of head recursion
"""
# 1. Base case
if n == 0:
return
# 2. Recurrence relation
print_count_ascending(n - 1)
# 3. Processing
print(n)
In [9]:
Copied!
print_count_ascending(5)
print_count_ascending(5)
1 2 3 4 5
In [ ]:
Copied!