Chapter Review Questions#
What are the two essential parts of every correct recursive function?
Python’s recursion limit.
What error does Python raise when the recursion limit is exceeded?
What is the default limit?
Trace
factorial(3)step by step, showing each recursive call and its return value.Naive recursive Fibonacci.
Why is the naive recursive Fibonacci slow for large
n?What is the time complexity in terms of the number of calls made?
Memoization with
lru_cache.What does
functools.lru_cachedo?How does it improve the performance of recursive Fibonacci?
Write a recursive function
sum_list(lst)that returns the sum of all integers in a list, without usingsum().Recursive GCD.
In the recursive GCD algorithm, what is the base case?
Why does the algorithm always terminate?
The call stack.
What is a call stack?
What happens to it as recursive calls are made?
What happens to it as those calls return?
When is recursion a better choice than an iterative loop? Give one concrete example.
The recursive binary search splits the problem in half at each step. What is the maximum recursion depth for a list of 1024 elements?
Inner helper pattern.
What is the advantage of putting the recursive
helperfunction inside the outerbinary_searchfunction instead of using default parameters likelo=0andhi=None?Can the caller accidentally pass a wrong value for
loworhighwhen using the inner helper pattern? Why or why not?
Property-based testing with Hypothesis.
What does
@given(st.lists(st.integers()), st.integers())tell Hypothesis to do?Name two properties that the binary search test verifies for every generated input.
Why is property-based testing more thorough than writing a fixed set of example inputs by hand?
Filesystem walker — observer pattern.
What four events does
FileSystemEventWalkerfire during a traversal?What does returning
Falsefromenter_dirdo?Write a handler class that counts the total number of files visited, without printing anything.
DFS vs BFS filesystem traversal.
In what order does depth-first traversal (DFS) visit entries compared to breadth-first traversal (BFS)?
DFS uses the call stack; BFS uses an explicit
deque. What practical consequence does this have for very deep directory trees?Give one scenario where BFS is the better choice over DFS.
Recursive descent parser — grammar mapping.
In the arithmetic grammar, why does
exprcalltermrather than handling multiplication itself?Each grammar rule becomes one method in the
Parserclass. Which method handles parenthesised sub-expressions, and how does it re-enter the grammar?What is mutual recursion, and where does it appear in the parser?
Associativity in the recursive descent parser.
Why does
expruse awhileloop butpoweruses a singleifwith a recursive call?Evaluate
2 ^ 3 ^ 2by hand, showing how right-associativity groups the operands.What would change in the output of
calc("8 / 4 / 2")if division were accidentally made right-associative?
Structural recursion in the evaluator.
What does it mean for recursion to be structural?
The
eval_exprfunction uses Pythonmatchstatements. What are the three cases it handles?Trace the evaluation of
BinOp(Number(2), '+', Number(3))througheval_expr, showing each recursive call and its return value.