Case-based lecture: you attempt, we discuss, then we reveal.
cs-045, cs-046, cs-129, cs-047, cs-130, cs-048A statistics function returns the wrong median for even-length input (right for odd). Diagnose; decide the empty case.
Expected: 3 for odd; 2.5 for even; empty: define or reject
Cost: O(n log n); selection algorithms (lecture 18's kth-element) do better - a teaser.
Readings [0, 5, 0, 7] with 'stop reading at 0'. The reference prints [] (eaten) vs [0, 5, 0, 7] (kept).
Expected: [] (broken) vs [0, 5, 0, 7] (fixed)
Cost: O(n) either way; the content is contract design.
Tuple-assignment swaps inside a loop that also reassigns indices: the reference prints [3, 2, 3] - one element duplicated, one lost. Diagnose with a memory diagram.
Expected: [3, 2, 3] (buggy) vs the intended swap after the fix
Cost: Constant; binding-order reasoning returns in cs-048 and cs-055.
Snippets: (a) counter defined outside a function, (b) shared default list, (c) range(len(xs)) deletion loop. Reference: crash 'list index out of range'; fixed [1, 5] and [1, 2] [1, 2].
def median(xs):
s = sorted(xs)
n = len(s)
mid = n // 2
if n % 2 == 1:
return s[mid]
return (s[mid - 1] + s[mid]) / 2
print(median([3, 1, 2]), median([4, 1, 3, 2]))Expected: buggy crashed: list index out of range | fixed: [1, 5] and [1, 2] [1, 2]
Cost: All O(n); three defect classes in one hour, each fixed under the full protocol.
A median function returns wrong values for even-length lists. Localise, fix, and add regression tests so it never regresses.
**Input:** [1, 3, 2] -> 2 ok (odd length, middle element); [1, 2, 3, 4] -> buggy returns 3, correct is 2.5**Output:** bug: takes the element at index len//2 even for even lengths; fix: average the two middle elementsSolve 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-045 — not embedded in this deck.
A sentinel value of 0 corrupts statistics for legitimate zeros in the data. Diagnose the sentinel choice and redesign the loop's exit.
**Input:** readings [0, 5, 0, 7] with sentinel 0 termination**Output:** sentinel stops at first 0: stats over [] or [partial]; real zeros miscounted - fix: read count first, then iterateSolve 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-046 — not embedded in this deck.
The exams office exports this file, `marks.csv`, exactly as written:
**Input:** the six-row table above, the office policy as given**Output:** `included 3, absent 2, recheck 1, mean 72.3`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-129 — not embedded in this deck.
An attempted in-place swap of list elements fails silently for one ordering. Trace it, find why, and enumerate every ordering in a test.
**Input:** swap a[i], a[j] via tuple assignment inside a loop that also reassigns indices**Output:** swap works syntactically; the defect is the index update order - enumerate all orderings in testsSolve 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-047 — not embedded in this deck.
Two registration exports must describe the same class of students.
**Input:** the two exports above**Output:** `unique students: 6` — plus the duplicate id(s) removed, theflagged id(s) with reasons, and the escalated ambiguitySolve 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-130 — not embedded in this deck.
Three small programs, each with a distinct classic defect class (scope, mutation, boundary). Localise all three under a time budget, then rank difficulty.
**Input:** three snippets: (a) counter defined outside a function, (b) shared default list, (c) range(len(xs)) deletion loop**Output:** (a) scope defect; (b) persistent-state defect; (c) skip-after-remove defectSolve 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-048 — not embedded in this deck.