Problem Solving — CS & DS

Lecture 13 — Dictionaries and Sets

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

The word tally: dictionary as counter

cs-049

Count 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.

Worked example

Course rosters: set operations as questions

cs-050

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).

Worked example

The LRU cache: recency as a moving queue

cs-051

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).

Case attempt · lecture 13
Case cs-049 · Beginner

The Word Tally

Count word frequencies in a paragraph and report the top three. The gateway dictionary-counting case, with tie-breaking discussion.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-049 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-049 — not embedded in this deck.

Case attempt · lecture 13
Case cs-050 · Foundational

Course Rosters

Given two course rosters, output students in both, either, and only one. Set operations as direct vocabulary.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-050 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-050 — not embedded in this deck.

Case attempt · lecture 13
Case cs-051 · Intermediate

The Cache Simulator

Simulate a tiny key-value cache with a size limit: inserting evicts the least-recently-used. Order plus dictionary state; eviction policy reasoning.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-051 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-051 — not embedded in this deck.

Case attempt · lecture 13
Case cs-132 · Intermediate

The Silent Roster

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.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-132 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-132 — not embedded in this deck.

Case attempt · lecture 13
Case cs-052 · Expert

The Consensus Checker

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.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-052 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-052 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. When do you choose a dictionary over a list?
  2. What single operation do sets make O(1)-ish and lists O(n)?
  3. Restate the consensus rule of cs-052 precisely.
Expected answers (instructor): when lookup is by meaningful key, not position membership testing (in) a decision passes only when agreement count meets the quorum threshold
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF