📖 Scenario: Imagine you are helping a friend understand two ways to solve problems: recursion and iteration. You will create simple examples to see when each way works better.
🎯 Goal: Build two small programs: one using recursion and one using iteration to count down numbers. Learn when recursion or iteration is easier or better.
📋 What You'll Learn
Create a recursive function to count down from a number to zero
Create an iterative loop to count down from the same number to zero
Compare outputs of both methods
Print the countdown results
💡 Why This Matters
🌍 Real World
Counting down is a simple example, but recursion is used in tasks like searching trees or solving puzzles, while iteration is common in loops and repeated actions.
💼 Career
Understanding when to use recursion or iteration helps in writing efficient code and solving problems clearly, a key skill for software developers.
Progress0 / 4 steps
1
Create a recursive countdown function
Define a function called countdown_recursive that takes a parameter n. Inside the function, if n is 0, return an empty list. Otherwise, return a list starting with n followed by the result of calling countdown_recursive(n - 1).
DSA Python
Hint
Use a base case when n is 0 to stop recursion. Otherwise, build the list by adding n and calling the function again with n - 1.
2
Create an iterative countdown function
Define a function called countdown_iterative that takes a parameter n. Inside the function, create an empty list called result. Use a for loop with variable i that goes from n down to 1 (inclusive). In each loop, append i to result. After the loop, return result.
DSA Python
Hint
Use a for loop counting down from n to 1. Add each number to the list and return it.
3
Set the starting number for countdown
Create a variable called start_number and set it to 5.
DSA Python
Hint
Just create a variable named start_number and assign it the value 5.
4
Print countdown results from both methods
Print the result of calling countdown_recursive(start_number). Then print the result of calling countdown_iterative(start_number).
DSA Python
Hint
Use print() to show the lists returned by both functions with start_number as input.
Practice
(1/5)
1. Which scenario is best suited for using recursion instead of iteration?
easy
A. When the problem requires simple repeated loops
B. When you want to avoid function call overhead
C. When memory usage must be minimal
D. When the problem can be divided into similar smaller problems
Solution
Step 1: Understand recursion use case
Recursion works best when a problem can be broken down into smaller similar problems, like tree traversals or factorial calculation.
Step 2: Compare with iteration scenarios
Iteration is better for simple repeated tasks without the need for breaking down problems.
Final Answer:
When the problem can be divided into similar smaller problems -> Option D
Quick Check:
Recursion = divide and conquer [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking iteration is always faster
Ignoring function call overhead in recursion
Confusing recursion with simple loops
2. Which of the following is the correct syntax for a recursive function in Python?
easy
A. def func(): return func()
B. def func(n): if n == 0: return 1 else: return n * func(n-1)
C. def func(n): while n > 0: n -= 1 return n
D. def func(n): for i in range(n): return i
Solution
Step 1: Identify recursive function structure
A recursive function calls itself with a smaller input and has a base case to stop recursion.
Step 2: Check each option
def func(n): if n == 0: return 1 else: return n * func(n-1) correctly defines a base case (n == 0) and recursive call (func(n-1)). Others either lack base case or use iteration.
Final Answer:
def func(n): if n == 0: return 1 else: return n * func(n-1) -> Option B
Quick Check:
Base case + self call = recursion [OK]
Hint: Look for base case and self call in function [OK]
Common Mistakes:
Missing base case causing infinite recursion
Using loops instead of recursive calls
Incorrect indentation or syntax errors
3. What is the output of the following Python code?
def factorial_iter(n):
result = 1
for i in range(1, n+1):
result *= i
return result
print(factorial_iter(4))