Case-based lecture: you attempt, we discuss, then we reveal.
cs-049, cs-050, cs-051, cs-132, cs-052Count how often each word occurs in "the cat the dog the bird cat", then name the most frequent word.
words = "the cat the dog the bird cat".split()
tally = {}
for w in words:
tally[w] = tally.get(w, 0) + 1
best = max(tally, key=tally.get)
print(tally["the"], tally["cat"])
print(best, tally[best])Expected: {'the': 3, 'cat': 2, 'dog': 1, 'bird': 1} and the (count 3)
Cost: O(n) time for n words, O(u) space for u distinct words - one pass, no nesting.
CS = {ada, bo, cy}; DS = {bo, dee, ada}. Who takes both? Either? Only CS? Only DS?
CS = {"ada", "bo", "cy"}
DS = {"bo", "dee", "ada"}
print(sorted(CS & DS))
print(sorted(CS | DS))
print(sorted(CS - DS))
print(sorted(DS - CS))Expected: both {ada, bo}; either {ada, bo, cy, dee}; only CS {cy}; only DS {dee}
Cost: Set operations are O(min(len)) on average; building sets is O(n).
Capacity 2. Operations: put a, put b, get a, put c. Which entries survive, and which is evicted next?
order = []
def use(k):
if k in order:
order.remove(k)
order.insert(0, k)
for op, k in [("put","a"),("put","b"),("get","a"),("put","c")]:
use(k)
if op == "put" and len(order) > 2:
order.pop()
print(order)
use("d")
order.pop()
print(order)Expected: after put c: {a, c} (b evicted); next eviction: a
Cost: O(capacity) per operation with a list; real caches use a hash map plus linked list for O(1).
Count word frequencies in a paragraph and report the top three. The gateway dictionary-counting case, with tie-breaking discussion.
**Input:** "the cat the dog the bird cat"**Output:** the:3, cat:2, dog:1, bird:1 -> top three: the(3), cat(2), then dog and bird tie at 1 (tie-break: alphabetical)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-049 — not embedded in this deck.
Given two course rosters, output students in both, either, and only one. Set operations as direct vocabulary.
**Input:** CS = {ada, bo, cy}; DS = {bo, dee, ada}**Output:** both: {ada, bo}; either: {ada, bo, cy, dee}; only CS: {cy}; only DS: {dee}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-050 — not embedded in this deck.
Simulate a tiny key-value cache with a size limit: inserting evicts the least-recently-used. Order plus dictionary state; eviction policy reasoning.
**Input:** capacity 2; operations: put a, put b, get a, put c (evicts b)**Output:** after put c: {a, c}; LRU order a (recent), c; then put d evicts aSolve 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-051 — not embedded in this deck.
The club attendance app shows **"0 attended"** for last week's session. There were definitely people in the room. The session lead wants to email everyone who "missed" it — tonight.
**Input:** the export column list; the session lead's request**Output:** three interpretations with their required columns, oneclarifying question, one safe email opening lineSolve 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-132 — not embedded in this deck.
Given ballots ranking three options, compute pairwise preferences and detect whether a majority prefers A over B and B over C while C beats A overall (a cycle). Fairness emerges from data.
**Input:** 3 ballots: A>B>C, B>C>A, C>A>B**Output:** A beats B 2-1; B beats C 2-1; C beats A 2-1 - a Condorcet cycle: no majority winnerSolve 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-052 — not embedded in this deck.