cs-038 — The Reversal Family
| Field | Value |
|---|---|
| id | cs-038 |
| difficulty | Foundational |
| topics | strings, slicing |
| lecture | lecture-10 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | lectures 1-2 (problem framing, precise pseudocode) |
| objective | recognise the pattern, translate it into precise steps, and trace it correctly |
| solution | held by the instructor; discussed after the attempt |
Problem
Reverse a word, then a sentence word-by-word (not letter-by-letter), then decide if a phrase is a palindrome ignoring spaces and case.
Your deliverable is the verdict for the worked instance, the normalisation rules you applied, and one input that exposes a missing rule. 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: "stressed" -> "desserts"; "the quick brown fox"; "Never odd or even"
- Output: desserts; fox brown quick the; palindrome (ignore case/spaces): yes
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
- Three tasks, three tools: slicing [::-1], split/join, and normalise-then-compare.
- Most string tasks are: normalise, then a simple check.
- Index arithmetic is where string bugs live — guard the ends first.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Which normalisation steps are safe to merge, and which change the answer if done in the wrong order?"
Constraints and assumptions
- Difficulty: Foundational (early-semester techniques assumed: precise statements, traces, simple loops).
- Topics: strings, slicing.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes; naming the pattern and stating the assumption is already a pass, completing the calculation is the stretch.
- 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: Normalise the input differently and show the answer does (or does not) change.