Case-based lecture: you attempt, we discuss, then we reveal.
cs-117, cs-118, cs-119, cs-120Posters cost 3 each and reach 40 students; flyers cost 1 and reach 10. Budget 12. Maximise reach. Write the objective, the constraint, and the decision variables - then solve.
best = 0
best_pf = (0, 0)
for p in range(13):
for f in range(13):
if 3 * p + f <= 12:
reach = 40 * p + 10 * f
if reach > best:
best, best_pf = reach, (p, f)
print(best, best_pf)Expected: maximum reach 160 with 4 posters, 0 flyers
Cost: O(budget^2) enumeration here; the formulation scales to solvers when the space explodes.
Three shifts, two staff (ada, bo), each shift needs exactly one person, nobody works two adjacent shifts, ada cannot take shift 1. Find any valid roster.
def solve():
for s1 in ("bo",):
for s2 in ("ada", "bo"):
for s3 in ("ada", "bo"):
roster = (s1, s2, s3)
if len(set(roster)) == 3 or roster[0] == roster[1] or roster[1] == roster[2]:
continue
if "ada" == roster[0]:
continue
return roster
return None
print(solve())Expected: valid roster: shift 1 bo, shift 2 ada, shift 3 bo
Cost: Exponential worst case, but pruning makes toy instances instant - the general shape behind schedulers and solvers.
You bid for one of two identical items against one rival. Your value 100, budget cap 80. Sealed bid. What should you bid - and which part of that question is actually optimisable?
best_bid, best_val = 0, -1
for b in range(81):
val = (100 - b) * b / 100
if val > best_val:
best_val, best_bid = val, b
print(best_bid, best_val)Expected: bid 50 (expected surplus 25 under the uniform-rival assumption)
Cost: O(budget) enumeration; closed-form parabola vertex exists (enrichment: calculus).
Maximise posters bought under a budget with two price options and a wall-length constraint. Write objective and constraints formally, then solve by hand.
**Input:** budget 20; posters A price 5 reach 40 each, B price 4 reach 30; wall space 3 m, A needs 1 m, B needs 0.5 m**Output:** formulation: max 40a+30b s.t. 5a+4b<=20, a+0.5b<=3, integers >=0; best by enumeration: a=2,b=2 -> 140Solve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-117 — not embedded in this deck.
Cover a 7-day rota with staff availability rules. Formulate; then solve a small instance by reasoning and a bigger one by search.
**Input:** 7 days, each day needs 2 staff; ada/bo/cy/dee available on subsets; max 4 days each**Output:** a feasible rota or proof none exists (try greedy by most-constrained day first)Solve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-118 — not embedded in this deck.
Two university-trip packing variants (must-fill weight vs must-stay-under) change the algorithm. Same story, different formulation; recognise which is which.
**Input:** items weight/value: (5, 30), (4, 26), (3, 14); capacity 8**Output:** 0/1 knapsack: best value 56 {(5,30)+(3,14)=44 no -> (4,26)+(3,14)=40? enumerate: (5,30)+(4,26) over; best = 30+14=44 or 26+14=40 -> 44; fractional would take 4/5 of firstSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-119 — not embedded in this deck.
Seat students so no two friends sit adjacent in a grid, or report impossible. Constraint search with pruning; when to admit failure honestly.
**Input:** 4x4 grid, friends pairs (r1,c1)-(r2,c2) must not sit adjacent (share an edge); 6 students, 2 friend pairs**Output:** a valid seating or 'impossible' - backtracking with adjacency pruning; report the search countSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-120 — not embedded in this deck.