cs-019 — The Broken Keypad
| Field | Value |
|---|---|
| id | cs-019 |
| difficulty | Intermediate |
| topics | digits, reconstruct |
| lecture | lecture-05 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | loops, lists, and tracing from lectures 1-6 |
| objective | choose the technique, trace it faithfully, and defend the boundary behaviour |
| solution | held by the instructor; discussed after the attempt |
Problem
A keypad digit is dead; given a target number, output all numbers up to N that avoid that digit. Careful digit checking with // and %.
Your deliverable is the stated assumptions, the digit-replacement rule, the rewritten number for the worked instance, and one boundary value (all digits affected, or none) traced. 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: dead digit 4, N = 30
- Output: 1,2,3,5,6,7,8,9,10,11,12,13,15,... (23 numbers below 30 avoid the digit 4)
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
- Two ways: check str(d) for the dead digit, or peel digits with % and // - implement both, compare.
- Writing the rules IS the solution; code is transcription.
- Every assumption is a place where two good answers can differ.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Propose one extra rule that would change the verdict — is it an improvement?"
Constraints and assumptions
- Difficulty: Intermediate (loops and lists assumed).
- Topics: digits, reconstruct.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes for the plan and trace; finishing the details is a bonus, defending the method is the target.
- 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: Find the closed-form answer and prove it agrees with your loop.