EXECUTED, WITH ASSERTIONS
This program was run during verification and its results asserted.
Straight from labs/course-3-python/03a_factorial_recursion.py, unchanged.
"""Experiment 3(a): Calculate the factorial of a number using recursion.
Syllabus: Course 3, Unit 2 -- recursive functions.
Sample input: 6
"""
# Step 1: The recursive function: a base case and a recursive case
def factorial(n):
"""Return n! computed recursively."""
if n < 0:
raise ValueError("factorial is undefined for negative numbers")
if n in (0, 1): # BASE CASE -- stops the recursion
return 1
return n * factorial(n - 1) # RECURSIVE CASE
# Step 2: The same function, printing each call
def factorial_traced(n, depth=0):
"""Same function, printing the call stack so you can trace it in a viva."""
indent = " " * depth
print(f"{indent}factorial({n}) called")
if n in (0, 1):
print(f"{indent} base case -> 1")
return 1
result = n * factorial_traced(n - 1, depth + 1)
print(f"{indent} returns {n} * factorial({n - 1}) = {result}")
return result
if __name__ == "__main__":
# Step 3: Read n, and print n! and the call trace
number = int(input("Enter a non-negative integer: "))
print(f"\n{number}! = {factorial(number)}\n")
print("Call trace:")
factorial_traced(number)
One experiment from the Python Programming and Data Structures lab. The rest of them, and the theory behind this one, are on the lab page.