Problem Solving — CS & DS

Lecture 10 — Strings Under the Lens

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

Username auditor: classifier with one character of memory

cs-037

Usernames valid when: 5-16 chars, letters/digits/underscores only, not starting with a digit, no two consecutive underscores. Audit the batch.

Expected: pass | fail: consecutive underscores | fail: starts with letter check | fail: too short

Cost: O(length) with early exits; the design content is the rule ORDER.

Worked example

The reversal family: three problems, one idea

cs-038

Reverse 'stressed' → 'desserts'; reverse words of 'the quick brown fox'; decide whether 'Never odd or even' is a palindrome.

Expected: desserts | fox brown quick the | True False

Cost: O(n) per operation; lecture 19 replaces slicing with two pointers for the palindrome.

Worked example

Template mail-merge: parse, fill, report

cs-039

Template 'Dear {name}, your {item} is due {deadline}.' When 'item' is missing, print '[item]' visibly rather than crashing.

Expected: Dear Ada, your [item] is due Friday. | Dear Bo, your book is due Monday.

Cost: O(template + record); accumulate-then-join, not repeated concatenation.

Worked example

The anagram detector: the normalisation ladder

cs-040

'Dormitory' / 'dirty room!'; 'astronomer' / 'moon starer'; 'hello' / 'world'. Reference: True, True, False.

for name in ("ab1", "abc"):
    print(name, name[0].isalpha() and name[-1].isdigit())

Expected: True | True | False

Cost: O(n log n) via sorting; counting is O(n) - both honest, the ladder is the transferable part.

Case attempt · lecture 10
Case cs-037 · Beginner

Username Auditor

Check usernames against rules: length, allowed characters, no consecutive underscores. Walk characters and build verdicts.

⏱ 5 minutes — attempt now

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

Discussion

cs-037 — discuss

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

Case attempt · lecture 10
Case cs-038 · Foundational

The Reversal Family

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.

⏱ 5 minutes — attempt now

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

Discussion

cs-038 — discuss

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

Case attempt · lecture 10
Case cs-039 · Intermediate

Template Mail-Merge

Fill 'Dear {name}, ... {deadline} ...' templates from records, handling missing fields with a fallback. Splitting, joining, and safe replacement.

⏱ 5 minutes — attempt now

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

Discussion

cs-039 — discuss

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

Case attempt · lecture 10
Case cs-040 · Advanced

The Anagram Detector

Decide if two phrases are anagrams ignoring case, spaces, and punctuation. Normalisation as the key idea; two implementation routes (sort vs count).

⏱ 5 minutes — attempt now

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

Discussion

cs-040 — discuss

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

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Why normalise before comparing strings?
  2. What does immutability of strings imply for building output?
  3. When is slicing clearer than a loop?
Expected answers (instructor): so equal-meaning strings compare equal (case, spaces, order) each change builds a new string; accumulate in a list and join when the operation is positional (reverse, prefix, step) rather than conditional
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF