Case-based lecture: you attempt, we discuss, then we reveal.
cs-085, cs-086, cs-087, cs-088Compute the digit sum of 49206 - recursively, then with iteration. Both must print the same number.
def dsum(n):
if n < 10:
return n
return n % 10 + dsum(n // 10)
def dsum_iter(n):
total = 0
while n > 0:
total += n % 10
n //= 10
return total
print(dsum(49206), dsum_iter(49206), dsum(0))Expected: 21, 21, and 0 for the empty case
Cost: O(digits) either way; recursion adds stack frames, iteration a variable.
Compute 3^13 by repeated squaring, not by twelve multiplications.
def power(x, e):
if e == 0:
return 1
h = power(x, e // 2)
if e % 2 == 0:
return h * h
return x * h * h
print(power(3, 13), 3 ** 13)Expected: 1594323 (matches the built-in power)
Cost: O(log e) multiplications; big integers make each multiply slower as values grow, but the count is the point.
Staircase of 8 steps; you climb 1 or 2 at a time. How many distinct ways to the top?
calls = {"plain": 0, "memo": 0}
def ways_plain(n):
calls["plain"] += 1
if n <= 2:
return n
return ways_plain(n - 1) + ways_plain(n - 2)
def ways_memo(n, cache={1: 1, 2: 2}):
calls["memo"] += 1
if n in cache:
return cache[n]
cache[n] = ways_memo(n - 1) + ways_memo(n - 2)
return cache[n]
print(ways_plain(8), ways_memo(8), calls["plain"], calls["memo"])Expected: 34 ways; plain recursion made 67 calls, memoised 15
Cost: Plain: O(2^n)-ish (golden-ratio precise). Memoised: O(n) time and space - this is dynamic programming arriving by fix-it ticket, formally next lecture.
Describe nested dolls recursively; write the function that counts layers. Base case and trust in the recursive call, made concrete.
**Input:** doll with 5 layers**Output:** count_layers: 0 for no doll, else 1 + inner; base case is the whole lessonSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-085 — not embedded in this deck.
Digit sum via loop vs recursion; then count the calls. Same result, different shapes; when recursion mirrors the problem's structure.
**Input:** n = 4723**Output:** digit sum 16 both ways; recursive version makes 4 calls; loop makes 4 iterationsSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-086 — not embedded in this deck.
Compute x^n with O(log n) multiplications by halving the exponent. Divide-conquer-combine, with a trace that shows the saving.
**Input:** 2^10 -> 4 multiplications (vs 9 naive); 2^100 -> ~7 (vs 99)**Output:** x^n = x^(n/2) squared; odd n costs one extra multiply; trace shows the savingSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-087 — not embedded in this deck.
Count ways to climb n steps taking 1 or 2 at a time. Recursion tree shows re-computation; memoisation saves it. The bridge to dynamic programming.
**Input:** ways(5) = 8 (1+1+1+1+1, 2+1+1+1, ... patterns); ways(1)=1, ways(2)=2**Output:** ways(n) = ways(n-1) + ways(n-2): Fibonacci in disguise; memo turns 2^n into O(n)Solve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-088 — not embedded in this deck.