Problem Solving — CS & DS

Lecture 15 — Counting, Combinatorics, and Choice

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 locker problem: divisors leave squares open

cs-057

100 lockers, 100 students; student i toggles every i-th locker. Which lockers end open?

open_lockers = [False] * 101
for student in range(1, 101):
    for locker in range(student, 101, student):
        open_lockers[locker] = not open_lockers[locker]
squares = [n for n in range(1, 101) if open_lockers[n]]
print(squares[:5], squares[-1], len(squares))

Expected: open lockers: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100

Cost: Simulation O(n log n) (harmonic sum); the mathematical answer is O(1) - reasoning beats brute force.

Worked example

Team photo: count first, then block

cs-058

5 students line up for a photo; P and Q refuse to stand together. How many acceptable lineups?

import math
total = math.factorial(5)
blocked = math.factorial(4) * 2
print(total, blocked, total - blocked)

Expected: 72

Cost: O(1) with the counting argument; enumerating 120 lineups is fine too but does not scale.

Worked example

The committee draw: complements and stars

cs-059

A committee of 4 from 6 CS and 4 DS students. How many committees include at least one of each programme?

import math
c = math.comb
total = c(10, 4)
forbidden = c(6, 4) + c(4, 4)
print(total, forbidden, total - forbidden)

Expected: 194

Cost: O(1) arithmetic via factorials; a brute-force enumeration would touch all 210 committees.

Worked example

The birthday question: product of complements

cs-060

How many people are needed for a shared-birthday chance above 50%, assuming 365 equally likely days and no leap days?

p = 1.0
n = 0
while p >= 0.5:
    n += 1
    p *= (366 - n) / 365
print(n, round(1 - p, 3))

Expected: 23 people (P(shared) ~ 50.7%)

Cost: O(answer) for the product, O(trials x n) for the simulation - both trivial here.

Case attempt · lecture 15
Case cs-057 · Beginner

The Locker Problem

100 lockers, 100 students, student i toggles every i-th locker. Which remain open? Counting by reasoning, then verify with a tiny simulation.

⏱ 5 minutes — attempt now

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

Discussion

cs-057 — discuss

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

Case attempt · lecture 15
Case cs-058 · Foundational

Team Photo Lineups

Count lineups of 5 students where two specific students refuse to stand together. Inclusion-exclusion in plain language first.

⏱ 5 minutes — attempt now

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

Discussion

cs-058 — discuss

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

Case attempt · lecture 15
Case cs-133 · Foundational

The Late Bus Ledger

The university shuttle poster says: **"a bus every ten minutes."** The operational facts:

⏱ 5 minutes — attempt now

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

Discussion

cs-133 — discuss

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

Case attempt · lecture 15
Case cs-059 · Intermediate

The Committee Draw

From 6 CS and 4 DS students, a 4-person committee must include at least one of each programme. Count compositions; then compute the probability a random draw satisfies it.

⏱ 5 minutes — attempt now

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

Discussion

cs-059 — discuss

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

Case attempt · lecture 15
Case cs-060 · Advanced

The Birthday Question

How many students until two likely share a birthday? Derive the complement product, compute thresholds, and confirm by simulation. The classic that teaches complement thinking.

⏱ 5 minutes — attempt now

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

Discussion

cs-060 — discuss

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

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. When is the complement trick the right tool?
  2. Permutations vs combinations in one phrase each.
  3. How does simulation back up counting?
Expected answers (instructor): when counting the desired set directly is tangled but its opposite is simple order matters / order does not many random trials approximate the counted probability
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF