cs-049 — The Word Tally
| Field | Value |
|---|---|
| id | cs-049 |
| difficulty | Beginner |
| topics | dictionaries, counting |
| lecture | lecture-13 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | none |
| objective | state inputs, outputs, and assumptions, then solve a small instance by hand |
| solution | held by the instructor; discussed after the attempt |
Problem
Count word frequencies in a paragraph and report the top three. The gateway dictionary-counting case, with tie-breaking discussion.
Your deliverable is the mapping/set answers for the worked instance, the key choice justified, and the tie-break rule stated. If the problem is ambiguous anywhere, state the assumption you are making and carry on — a stated assumption is part of a good answer.
Examples
- 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 one number, name, or condition were replaced by something stranger.
What to notice
- Count with a dict; when reporting top-k, ties must be broken by a stated rule.
- A dictionary turns 'search for the item' into 'look it up' — that is the whole win.
- Sets answer yes/no questions; dictionaries answer how-many questions.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Re-derive the tie-break rule: is it a technicality or part of the specification?"
Constraints and assumptions
- Difficulty: Beginner (no prior experience assumed).
- Topics: dictionaries, counting.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes for a first attempt; partial answers with clear reasoning are worth more than silent struggling.
- Optional stretch (not required): describe an alternative approach and say when it beats the obvious one.
Hints and extension
- First nudge: identify the pattern the worked instance uses before computing anything.
- If stuck after three minutes, write the input/output line you expect and trace the smallest example by hand.
- After the reveal, attempt the extension below before the next lecture.
- Extension challenge: Invert the lookup (value to keys) and state when collisions appear.