Problem Solving — CS & DS

Lecture 04 — First Python: State and Sequences

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

Echo with limits, translated from pseudocode

cs-013

Read lines until the sentinel 'done'; echo each line uppercased, but skip lines longer than 20 characters (count them instead).

lines = ["coffee", "x" * 21, "tea", "done", "late"]
skipped = 0
for line in lines:
    if line == "done":
        break
    if len(line) > 20:
        skipped += 1
    else:
        print(line.upper())
print(skipped)

Expected: COFFEE then 1

Cost: O(total input length). Python: `while True:` + `break` mirrors the flowchart's loop with two exits.

Worked example

Course list cleaner: first lists, first filters

cs-014

Given a list of course codes, remove empty strings and duplicates (keeping first occurrences), and report how many entries were dropped.

Expected: ['CS1', 'MA2'] 2

Cost: O(n^2) with the list-membership check; the set version (lecture 13) is O(n) - say so, then wait.

Worked example

The change maker: state and arithmetic

cs-015

Price 2.35. The customer pays with coins 1.00, 1.00, 0.50. Compute credit as coins arrive, then change as 20/10/5-cent breakdown.

Expected: credit 250, change 15 -> [10, 5]

Cost: O(coins + denominations). The greedy breakdown works for this coin system - lecture 23 returns to when greedy is trustworthy.

Worked example

The meeting scheduler: constraints first

cs-016

Place requests into 5 slots, 1 hour each, max 2 meetings per slot, respect 'not on Friday'. Print the schedule and the refused requests.

Expected: schedule slots 1-4 used; refused: [Friday request]

Cost: O(requests x slots). Greedy first-fit is fine here; question for fast finishers: when does first-fit waste capacity?

Case attempt · lecture 04
Case cs-013 · Beginner

Echo with Limits

Read numbers until a sentinel 0, then output how many were read, their sum, and the largest. First translation from pseudocode to real Python.

⏱ 5 minutes — attempt now

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

Discussion

cs-013 — discuss

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

Case attempt · lecture 04
Case cs-014 · Foundational

Course List Cleaner

Given a list of course names with stray spaces and duplicates, produce a clean numbered list. Introduces list traversal and building output.

⏱ 5 minutes — attempt now

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

Discussion

cs-014 — discuss

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

Case attempt · lecture 04
Case cs-015 · Intermediate

The Change Maker

Compute change in coins (25/10/5/1) for an amount. Emphasise state updates of remaining amount and each coin count, and why order of subtraction matters.

⏱ 5 minutes — attempt now

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

Discussion

cs-015 — discuss

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

Case attempt · lecture 04
Case cs-016 · Advanced

The Meeting Scheduler

Given busy intervals and a meeting length, output all possible start times in a day. First encounter with walking through sorted intervals.

⏱ 5 minutes — attempt now

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

Discussion

cs-016 — discuss

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

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. What does predict-then-run change about learning?
  2. Name the three Python errors you met today and their causes.
  3. Why convert input strings before arithmetic?
Expected answers (instructor): prediction makes the run informative; mismatches are the lesson e.g. NameError (typo/undefined), ValueError on int('3.5'), IndexError on range end input() returns strings; arithmetic on them concatenates or fails
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF