Module 18 · Algorithms and Problem Solving — topic 13 of 18
Recursion problems
Solving problems recursively: base cases and recursive cases, classic problems (factorial, digit sums, reversing strings, fast power, flattening nested lists, permutations, Tower of Hanoi), and recognising when recursion fits.
Learning objectives
- Design base and recursive cases
- Solve classic recursive problems
- Recognise problems with naturally recursive structure
Definition
A recursive solution solves a problem by solving smaller instances of the same problem. It needs a base case that is answered directly and a recursive case that reduces the problem towards the base case.
Introduction
Some problems are defined in terms of themselves: a folder contains files and folders, a permutation is a choice followed by a permutation of the rest. Recursion matches that structure directly.
Why it matters
Trees, nested data, backtracking searches and divide-and-conquer algorithms are much clearer recursively than with loops.
Detailed explanation
Every recursive function needs at least one base case that returns without recursing — otherwise it recurses until RecursionError.
The recursive case must make the problem smaller: a shorter list, a smaller number, a sublist.
Trust the recursion: assume the recursive call returns the right answer for the smaller problem and focus on combining it.
Divide and conquer splits a problem into parts, solves each recursively and combines the results (fast power, merge sort).
Recursion fits nested structures naturally: nested lists, folder trees, JSON documents.
Backtracking problems (permutations, puzzles) explore choices recursively.
Python limits recursion depth (about 1,000 by default) and doesn't optimise tail calls, so very deep recursion should be rewritten as a loop.
Repeated subproblems (like naive Fibonacci) make recursion exponentially slow; memoisation fixes this.
Syntax
- Always have a base case.
- Make the problem smaller.
- Trust the recursive call.
Code examples
Factorial and digit sums
720 35
Reversing and fast power
noisrucer 1024 243
More examples
Nested lists, permutations and Hanoi
[1, 2, 3, 4, 5] ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] 7 ['A->C', 'A->B', 'C->B']
Step-by-step explanation
- flatten recurses only into elements that are lists, however deeply nested.
- Each permutation is a first character followed by a permutation of the remaining characters.
- Hanoi moves n − 1 discs out of the way, moves the largest, then moves the n − 1 discs back on top: 2ⁿ − 1 moves in total.
Expected output
Running “Nested lists, permutations and Hanoi” prints exactly:
Practical applications
- Walking folder trees and parsing nested JSON.
- Solving puzzles such as Sudoku with backtracking.
Best practices
- Write the base case first.
- Use recursion for naturally recursive structures.
- Switch to loops or memoisation when depth or repetition grows.
Common mistakes
No reachable base case
Avoid
Prefer
Without a base case the function recurses until RecursionError.
Important notes
- sys.getrecursionlimit() shows the current depth limit.
Key takeaways
- Recursion needs a base case and a smaller subproblem.
- It suits nested data, divide and conquer and backtracking.
- Watch depth limits and repeated work.
Try it yourself
- 1Write a recursive function that counts the files in a nested dictionary representing folders.
- 2Generate all subsets of a list recursively.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- AdvancedWhat is printed?