Case-based lecture: you attempt, we discuss, then we reveal.
cs-069, cs-070, cs-071, cs-134, cs-072Players: ada 90, bo 75, cy 90. Rank by score, highest first; ties broken alphabetically.
players = [("ada", 90), ("bo", 75), ("cy", 90)]
ranked = sorted(players, key=lambda p: (-p[1], p[0]))
print(ranked)Expected: ada (90), cy (90), bo (75)
Cost: O(n log n) comparisons - the sorting cost dominates; the key is computed once per element.
Two rooms' meeting start times are already sorted: 9, 10, 12 and 9.5, 11. Produce one sorted list.
a = [9, 10, 12]
b = [9.5, 11]
merged = []
i = j = 0
while i < len(a) and j < len(b):
if a[i] <= b[j]:
merged.append(a[i])
i += 1
else:
merged.append(b[j])
j += 1
merged.extend(a[i:])
merged.extend(b[j:])
print(merged)Expected: 9, 9.5, 10, 11, 12
Cost: O(n + m) - linear in the output size; no element is compared twice.
Points (0,0), (3,4), (1,1). Which two are closest?
points = [(0, 0), (3, 4), (1, 1)]
best = None
for i in range(len(points)):
for j in range(i + 1, len(points)):
dx = points[i][0] - points[j][0]
dy = points[i][1] - points[j][1]
d = (dx * dx + dy * dy) ** 0.5
if best is None or d < best[0]:
best = (d, points[i], points[j])
print(best[1], best[2])Expected: closest pair: (0, 0) and (1, 1)
Cost: O(n^2) pairs - fine for a classroom, hopeless for a million points; knowing WHERE the wall is, is the lecture's point.
Sort players by score, then by name for ties. Stable sorting and composite keys; ties as a first-class concern.
**Input:** scores [(bo,90), (ada,95), (cy,90), (dee,88)]**Output:** sorted: ada 95, bo 90, cy 90, dee 88 (ties alphabetical: bo before cy - stable multi-key)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-069 — not embedded in this deck.
Merge overlapping meeting intervals. Sort first, then single scan with a merge rule; why sorting makes the scan trivial.
**Input:** meetings [(1,4), (2,3), (5,7), (6,8)]**Output:** merged: [(1,4), (5,8)]; (2,3) inside (1,4); (6,8) overlaps (5,7)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-070 — not embedded in this deck.
Find the two closest points on a line. Sort once, scan adjacent pairs. Then discuss: what breaks in two dimensions? (Motivation, not implementation.)
**Input:** points [1.5, 3.1, 9.2, 9.9, 4.0] -> closest pair (9.2, 9.9) distance 0.7**Output:** sort by value, then scan adjacent pairs only; (3.1, 4.0) is 0.9 apart — checking every pair would also find it, but slowerSolve 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-071 — not embedded in this deck.
A weather station posts one reading per minute. The dashboard must flag any minute whose value exceeds the **average of the previous three minutes** (the three before it, not counting itself) by more than 10. Minutes with an incomplete window are never flagged.
**Input:** the twelve readings, k = 3, threshold 10**Output:** flags at indices 4 and 8 (values 30 and 45) — day costA: 4320 additions, B: 1440 updatesSolve for the worked instance first. Then ask what would change if onePseudocode, 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-134 — not embedded in this deck.
Find the k-th smallest without fully sorting (partial selection via repeated min-removal on paper, then discuss doing better). Analysis reasoning from first principles.
**Input:** [7, 1, 5, 3, 9, 2], k = 3 -> answer 3; k = 1 -> 1; k = 6 -> 9**Output:** kth smallest via sort is O(n log n); repeated-min selection is O(kn) - compare on n=10000, k=3Solve 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-072 — not embedded in this deck.