Problem Solving — CS & DS

Lecture 25 — Thinking with Data

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 Canteen Queue Log: from log to ledger

cs-097

Log lines like '0812 ada lunch 6.50', '0815 bo snack 2.00', '0809 ada lunch 4.00'. Build per-person totals and the busiest item - decide your cleaning rules first.

lines = ["0812 ada lunch 6.50", "0815 bo snack 2.00", "0809 ada lunch 4.00"]
totals, items = {}, {}
for ln in lines:
    _t, person, item, amt = ln.split()
    totals[person] = totals.get(person, 0.0) + float(amt)
    items[item] = items.get(item, 0) + 1
busiest = sorted(items.items(), key=lambda kv: (-kv[1], kv[0]))[0][0]
print(round(totals["ada"], 2), busiest)

Expected: ada total 10.50; busiest item 'lunch' (2 visits)

Cost: O(n) for n log lines; the ranking is O(u log u) over distinct items.

Worked example

Screen Time by Cohort: group, then compare honestly

cs-098

(cohort, minutes) pairs: (A, 120), (B, 90), (A, 180), (B, 110), (A, 60). Compare mean screen time per cohort - and state what the comparison cannot tell you.

rows = [("A", 120), ("B", 90), ("A", 180), ("B", 110), ("A", 60)]
groups = {}
for c, m in rows:
    groups.setdefault(c, []).append(m)
for c in sorted(groups):
    v = groups[c]
    print(c, len(v), sum(v) / len(v))

Expected: A: mean 120 (n=3); B: mean 100 (n=2)

Cost: O(n) grouping, O(g) reporting - trivial cost, the care is free but must be spent.

Worked example

The Churn Story: pivot and percentage, denominator stated

cs-099

Cohorts: A: 40 stayed, 10 left; B: 25 stayed, 25 left. Which cohort has the worse churn rate, and what exactly does the percentage measure?

cohorts = {"A": (40, 10), "B": (25, 25)}
for c, (stayed, left) in sorted(cohorts.items()):
    total = stayed + left
    print(c, f"{left / total:.0%}", total)

Expected: A 20% churn, B 50% churn (denominator: cohort members)

Cost: O(1) per cohort; the intellectual work is in the denominator, not the arithmetic.

Case attempt · lecture 25
Case cs-097 · Beginner

The Canteen Queue Log

From a queue-length log, answer: busiest hour, average wait proxy, quietest day. Framing questions before computing.

⏱ 5 minutes — attempt now

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

Discussion

cs-097 — discuss

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

Case attempt · lecture 25
Case cs-098 · Foundational

Screen Time by Cohort

Compare app usage across three student cohorts with counts, shares, and a distribution sketch. Choosing summaries that answer the actual question.

⏱ 5 minutes — attempt now

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

Discussion

cs-098 — discuss

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

Case attempt · lecture 25
Case cs-099 · Intermediate

The Churn Story

Monthly subscriber counts in and out. Define churn rate precisely (what's the base?), compute, and explain a month that 'looks bad but isn't'.

⏱ 5 minutes — attempt now

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

Discussion

cs-099 — discuss

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

Case attempt · lecture 25
Case cs-139 · Intermediate

The Attendance Frame

The library reports: *"Weekday visits fell 20% the week after the deadline-week survey."* Two staff proposals:

⏱ 5 minutes — attempt now

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

Discussion

cs-139 — discuss

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

Case attempt · lecture 25
Case cs-100 · Expert

The Survey Skew

A survey over-represents one hall of residence. Show how naive averages mislead, then reweight (or segment) to a fairer summary.

⏱ 5 minutes — attempt now

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

Discussion

cs-100 — discuss

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

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Why state the decision before the computation?
  2. Why is the average alone dangerous in cs-098?
  3. What made cs-100's survey skew?
Expected answers (instructor): the decision determines which aggregate is meaningful distributions with the same mean differ; skew hides in the shape voluntary response — collection bias, not analysis error
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF