← Home
Python Recursion Dry Run Practice
Recursion rewards stack-frame discipline. Write each call's parameters and return before moving up the chain.
Example problems
Factorial
def f(n):
if n <= 1: return 1
return n * f(n-1)
print(f(4))Output: 24
Fibonacci
def fib(n):
if n < 2: return n
return fib(n-1) + fib(n-2)
print(fib(5))Output: 5
Countdown
def g(n):
print(n, end='')
if n: g(n-1)
g(2)Output: 210