Problem Solving — CS & DS

Lecture 16 — Mid-Course Consolidation

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 gradebook audit: aggregates with honesty

cs-061

Lines 'ada,CS1,80', 'bo,DS1,' (missing), 'ada,CS1,95', 'cy,CS1,300' (over 100). Produce per-student averages plus flags for missing and anomalous scores.

lines = ["ada,CS1,80", "bo,DS1,", "ada,CS1,95", "cy,CS1,300"]
missing, anomaly, acc = [], [], {}
for ln in lines:
    name, course, score = [p.strip() for p in ln.split(",")]
    if score == "":
        missing.append(name)
        continue
    s = float(score)
    if not 0 <= s <= 100:
        anomaly.append((name, s))
        continue
    acc.setdefault(name, []).append(s)
avg = {k: sum(v) / len(v) for k, v in acc.items()}
print(avg["ada"], missing, anomaly)

Expected: ada 87.5; missing: bo; anomaly: cy with 300

Cost: O(n) for n lines - one pass, flags accumulate alongside.

Worked example

Text stats on a budget: the O(n) reflex

cs-062

For "Data science is twenty percent practice and eighty percent cleaning data." report word count, reading time (200 wpm), the longest word, and the two most frequent words - in one pass.

text = "Data science is twenty percent practice and eighty percent cleaning data."
words = text.split()
n = len(words)
longest = max(words, key=len)
tally = {}
for w in words:
    tally[w] = tally.get(w, 0) + 1
top = sorted(tally.items(), key=lambda kv: (-kv[1], kv[0]))[:2]
print(n, round(n / 200 * 60, 1), longest)
print(top)

Expected: words 11; reading time 3.3 s; longest 'practice'; top: percent(2), then a one-count tie

Cost: O(n) time, O(u) space; the naive alternative (recount per aggregate) is still O(n) but four passes.

Worked example

The campus survey: coding scheme as specification

cs-063

Answers 'yes', 'no', 'YES!!', '', 'maybe, idk', and multi-select '1,3'. Define a coding scheme and apply it.

def code(answer):
    a = answer.strip().strip("!?").lower()
    return {"yes": 1, "no": 0}.get(a, None)

samples = ["yes", "no", "YES!!", "", "maybe, idk", "1,3"]
coded = [code(s) for s in samples]
print(coded)

Expected: yes=1, no=0, YES!!=1, ''=missing, 'maybe, idk'=missing, '1,3'=missing (not multi-select here)

Cost: O(n) over answers; the scheme costs nothing at runtime because it was decided at design time.

Worked example

The mini challenge cup: two problems, one clock

cs-064

Challenge 1: sum the digits of 2^15. Challenge 2: estimate the longest run of heads in 100 fair coin flips. Five minutes for both.

big = 2 ** 15
s = sum(int(d) for d in str(big))
print(s)

Expected: 26; typical longest heads-run in 100 flips: 6-7

Cost: Digit sum O(digits). Streak simulation O(trials x 100); exact DP exists but is overkill here.

Case attempt · lecture 16
Case cs-061 · Beginner

The Gradebook Audit

From raw score lines (student, course, score) build a per-student average and flag anomalies. Integrates parsing, dicts, aggregation, and a written policy for anomalies.

⏱ 5 minutes — attempt now

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

Discussion

cs-061 — discuss

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

Case attempt · lecture 16
Case cs-062 · Foundational

Text Stats on a Budget

Compute reading-time, longest word, and word-frequency histogram for a text without importing anything beyond basics. Multi-part but small; the skill is planning.

⏱ 5 minutes — attempt now

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

Discussion

cs-062 — discuss

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

Case attempt · lecture 16
Case cs-141 · Foundational

The Rollout Memo

Your team's script auto-refunds campus print credit under this rule — it is correct and deployed:

⏱ 5 minutes — attempt now

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

Discussion

cs-141 — discuss

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

Case attempt · lecture 16
Case cs-063 · Intermediate

The Campus Survey

Given messy survey responses (free text, multi-select), define a coding scheme, then count. The task is as much designing the representation as coding it.

⏱ 5 minutes — attempt now

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

Discussion

cs-063 — discuss

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

Case attempt · lecture 16
Case cs-064 · Advanced

The Mini Challenge Cup

Two previously unseen small problems, 10 minutes each, in exam conditions. Then a guided self-review with a rubric. Consolidation as rehearsal, plus a look ahead to Block III.

⏱ 5 minutes — attempt now

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

Discussion

cs-064 — discuss

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

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Name the three stations and why you chose yours.
  2. What must an integration plan name before code?
  3. What does exam pacing change about case attempts?
Expected answers (instructor): tracing / data structures / plan-first; chosen by personal weak spot the techniques, their order, and the data each stage hands on time-boxing: a complete partial plan beats an unfinished perfect one
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF