Problem Solving — CS & DS

Lecture 02 — Pseudocode as Precision

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

Largest of three, at three precision levels

cs-005

Read three numbers; output the largest. Make the pseudocode precise enough that a classmate translates it without questions.

Expected: 12 (and 7 for the tie case)

Cost: n - 1 comparisons for n numbers; can 2 compares ever suffice for 3 numbers? (No - a pigeonhole argument students can find themselves.)

Worked example

Password rules: order of tests

cs-006

Password valid when: 8-12 characters, at least one digit, starts with a letter. Output exactly one message naming the FIRST failing rule.

Expected: four different messages for the four traces

Cost: O(length) for the digit scan; the rest constant.

Worked example

The vending machine as a state machine

cs-007

Pseudocode for buying a snack: insert coins until credit >= price, then vend and give change; refuse when sold out (before accepting money).

Expected: vend, change 0.20, stock 2 | sold out, coins returned (stock 0 case)

Cost: O(coins inserted). The real content is the state table: credit and stock per event.

Worked example

Shuffle with a no-adjacent-artist rule

cs-008

Playlist A(pop), B(pop), C(jazz), D(pop), E(jazz). Order all songs once so no two same-artist songs are adjacent; state the repair rule when impossible.

a, b, c = 7, 12, 9
best = a
if b > best:
    best = b
if c > best:
    best = c
print(best)

Expected: A, C, B, E, D

Cost: O(n) passes for the interleave; the feasibility bound is the intellectual content.

Case attempt · lecture 02
Case cs-005 · Beginner

Largest of Three

Read three numbers and output the largest. Trivial in words — the value is in writing pseudocode precise enough that a classmate could translate it without questions.

⏱ 5 minutes — attempt now

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

Discussion

cs-005 — discuss

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

Case attempt · lecture 02
Case cs-006 · Foundational

Password Rules

A password must be 8-12 characters, contain at least one digit, and start with a letter. Express the check as pseudocode with clear order of tests and a single output message.

⏱ 5 minutes — attempt now

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

Discussion

cs-006 — discuss

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

Case attempt · lecture 02
Case cs-007 · Intermediate

The Vending Machine Script

Write pseudocode for buying a snack: insert coins until the price is met, handle overpayment with change, handle 'sold out'. Track machine state precisely.

⏱ 5 minutes — attempt now

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

Discussion

cs-007 — discuss

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

Case attempt · lecture 02
Case cs-008 · Expert

Shuffling a Playlist

Write pseudocode for a shuffle that plays every song exactly once but never two songs by the same artist back-to-back. The interest is in stating constraints and repair steps precisely.

⏱ 5 minutes — attempt now

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

Discussion

cs-008 — discuss

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

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Name the three control shapes every pseudocode solution is built from.
  2. What makes a pseudocode line 'testable'?
  3. When is a state table required?
Expected answers (instructor): SEQUENCE, IF/ELSE selection, WHILE/FOR iteration it names variables and values precisely enough to hand-trace whenever a later statement depends on more than the current input line
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF