Case-based lecture: you attempt, we discuss, then we reveal.
cs-077, cs-078, cs-079, cs-080How many 4-digit codes (0000 to 9999) have digit sum exactly 12?
count = 0
for code in range(10000):
if sum(int(d) for d in f"{code:04d}") == 12:
count += 1
print(count)Expected: 415 codes
Cost: O(10^4 x 4) - constant here, but the pattern is exponential in the number of dials: k dials -> 10^k. Brute force wins while the space stays small.
A perfect number equals the sum of its proper divisors (6 = 1+2+3). Find all perfect numbers below 10000.
def is_perfect(n):
total = 1
d = 2
while d * d <= n:
if n % d == 0:
total += d + n // d
d += 1
return n > 1 and total == n
print([n for n in range(2, 10000) if is_perfect(n)])Expected: [6, 28, 496, 8128]
Cost: O(n * sqrt(n)) overall - about 1.3 million operations here; the naive version needs 50 million. Same answer, 40x cheaper.
From values 3, 5, 2, 8, find a subset summing to 10 - or prove none exists.
items = [3, 5, 2, 8]
target = 10
found = None
for mask in range(1 << len(items)):
total = sum(items[i] for i in range(len(items)) if mask >> i & 1)
if total == target:
found = [items[i] for i in range(len(items)) if mask >> i & 1]
break
print(found)Expected: subset found: 3 + 5 + 2 = 10
Cost: O(2^n) time - the price of the proof; meet-in-the-middle or DP (later lectures) trade that certainty for speed.
Enumerate all 4-digit PINs satisfying 'digits sum to 12'. Count the space first, then enumerate. When brute force is the whole solution.
**Input:** 4-digit PINs, digits 0-9, digits sum to 12**Output:** count first: 10^4 = 10000 candidates; favourable = 415 (455 sums possible minus the four all-identical digits)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-077 — not embedded in this deck.
Find all perfect numbers below 10000 (equal to sum of proper divisors). Brute force with a smarter divisor bound; measure the improvement.
**Input:** perfect numbers below 10000 (sum of proper divisors equals n): 6, 28, 496, 8128**Output:** naive O(n^2); divisor bound sqrt(n) makes it ~O(n sqrt n) - measure bothSolve 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-078 — not embedded in this deck.
Find three people who can all attend the same slot from availability masks. Enumerate triples systematically and prune; estimate cost honestly.
**Input:** 4 people, 3 slots; free masks: ada {1,2}, bo {2,3}, cy {1,3}, dee {1,2,3}**Output:** triples with a common slot: {ada,bo,cy} -> none! check: intersection {2}&{3}&{3}... enumerate all 4 triplesSolve 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-079 — not embedded in this deck.
Pick numbers from a list to hit an exact target sum. Full enumeration is 2^n; show the cost curve, solve small n, then sketch why cleverness is needed for big n.
**Input:** [3, 7, 1, 2, 9], target 12 -> 3+9 or 1+2+9... enumerate subsets**Output:** 2^5 = 32 subsets; subset-sum found: {3, 9} and {1, 2, 9}; cost curve doubles each +1 itemSolve 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-080 — not embedded in this deck.