Case-based lecture: you attempt, we discuss, then we reveal.
cs-065, cs-066, cs-067, cs-068Daily temperatures are 12, 8, 3, -1, -4, 2 degrees. On which day index does the first freeze (temperature at or below 0) occur?
temps = [12, 8, 3, -1, -4, 2]
idx = None
for i, t in enumerate(temps):
if t <= 0:
idx = i
break
print(idx, temps[idx])Expected: first freeze at index 3 (temperature -1)
Cost: O(n) worst case, but best case O(1) when the freeze is early; early exit is free - take it.
A catalogue holds (code, title) pairs in no particular order. Find the title for code 'DS2', and define the behaviour for an unknown code.
catalogue = [("CS1", "Problem Solving"), ("DS2", "Statistics"), ("CS3", "Systems")]
def find(code):
for c, title in catalogue:
if c == code:
return title
return "not offered"
print(find("DS2"), find("XX9"))Expected: Statistics / not offered
Cost: O(n) time, O(1) space; already optimal for unsorted input.
In the values 1, 3, 8, 4, 2 find a peak - a value at least as large as its neighbours.
a = [1, 3, 8, 4, 2]
peak = None
for i in range(len(a) - 1):
if a[i] >= a[i + 1]:
peak = i
break
if peak is None:
peak = len(a) - 1
print(a[peak])Expected: a peak is 8 (at index 2)
Cost: O(n) here; lecture 21 revisits this problem with binary search for O(log n) - a first taste of the same problem having algorithms of different cost.
Given daily temperatures, find the first day below freezing, or report none. Linear scan with an early exit and a correct 'not found' result.
**Input:** temps [4, 2, 1, -1, 3], freeze below 0**Output:** first freeze on day 4 (index 3); none -> report -1 explicitlySolve 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-065 — not embedded in this deck.
Find all courses taught in a room with capacity at least X and free on Tuesdays. Filtering as a search with a compound predicate.
**Input:** courses [(CS1, room 40, cap 30, Tue), (DS1, room 12, cap 60, Wed), (AI, room 41, cap 45, Tue)], cap >= 40, free Tue**Output:** [DS1? no - Wed] -> matches: AI only (room cap 45, Tue); DS1 has cap but wrong daySolve 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-066 — not embedded in this deck.
A list is two sorted runs concatenated. Find the smallest element. Shows how preconditions (knowing structure) change the algorithm.
**Input:** [7, 9, 11, 2, 4, 6] (two sorted runs); minimum is 2**Output:** 2 at the run boundary; a plain min() scan works always - the structure allows betterSolve 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-067 — not embedded in this deck.
Find a local maximum in a list (greater than its neighbours). Comparing neighbours safely at the edges; why 'a peak exists' needs an argument.
**Input:** [1, 3, 4, 3, 1] -> peak at index 2 (value 4); edge peaks: [5,2] peak at 0; [2,5] peak at 1**Output:** returns any one local maximum index; a peak always exists (argument: climb uphill from anywhere)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-068 — not embedded in this deck.