Unit 3 · Programming 2 · Lesson 3.9

The Canonical List Algorithms

Max, min, average, count-if. Four algorithms the exam names by heart — and all four are the accumulator wearing different hats. Build them from scratch and you own a whole category of questions.

Big Idea 3 — Algorithms & ProgrammingNamed in the CED · MCQ staples

Before this lesson — read on Runestone

Watch how each algorithm is just a traversal with a different update rule. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Still in Programs. Traversal (3.8) walks a list; the accumulator (3.4) tracks a result. Today those combine into the four algorithms the CED names explicitly — the ones that show up again and again in multiple choice.

2

The Hook

Highest score. Lowest. Average. How many passed. Every one of those is something you’d ask about a gradebook — and every one is a single traversal with a different rule for what to keep.

The exam loves these because they’re short, they combine everything, and they have one classic trap: starting the accumulator at the wrong value. Get the setup right and the loop is easy.

3

The Idea

All four share the accumulator’s three positions — only the initialize and update change:

AlgorithmInitializeUpdate (each element x)Use after
sumtotal = 0total += xtotal
count-ifcount = 0if test: count += 1count
maxbiggest = list[0]if x > biggest: biggest = xbiggest
averagetotal = 0total += xtotal / len
The max/min initialization trap. It’s tempting to start biggest = 0. But if every value is negative — say [-5, -2, -9] — nothing ever beats 0, so the program wrongly reports 0 instead of -2. The fix: initialize to the first element of the list, list[0], which is guaranteed to be a real value. Same logic for min.

Python — max from scratch

biggest = nums[0]   # NOT 0
for x in nums:
    if x > biggest:
        biggest = x
print(biggest)

AP Pseudocode

biggest ← nums[1]   /* first element */
FOR EACH x IN nums
{ IF (x > biggest)
  { biggest ← x } }
DISPLAY(biggest)

Hover or tap a line to light its twin. Note the index flip in line 1: Python’s first element is nums[0], AP’s is nums[1] — but both mean “start from a real value in the list,” not 0.

4

Try It — Four Algorithms, One List

Enter a list and see all four results at once — plus a live demo of the max trap: compare the correct max to the buggy “start at 0” version. Try a list of all negatives.

list:
Presets:
5

Vocabulary

count-if Programs
A counting accumulator that adds 1 only when an element passes a test.
max / min algorithm Programs
A traversal that keeps the largest (or smallest) value seen so far — initialized to the first element, never to 0.
average Programs
A sum divided by the count of elements (total / len).
6

Check

Five questions, several in pseudocode. Watch the initialization especially — that’s the trap. Then pick your answer.

0 of 5 answered
7

Impact Check

Four little loops, running the dashboards

Highest temperature this week, lowest price found, average rating, how many reviews are 5 stars — every stat on every dashboard is one of these four algorithms over a list. Sports records, weather extremes, class averages, “how many liked this”: max, min, average, count-if, again and again.

The initialization trap is a real-world bug, too. A temperature tracker that starts lowest = 0 will never report a below-zero reading — it’ll insist the coldest day was 0°. Starting from a real data point instead of a convenient number is a habit that separates code that works on your test case from code that works on everyone’s data.

8

Connections

Came from 3.4 + 3.8 — each algorithm is traversal plus an accumulator with its own update rule.
Next in 3.10 — the gradebook lab runs all four on one list of scores.
Returns in 3.14 — you’ll wrap one of these in a procedure with a parameter — the exact Create PT shape.
Returns on the exam — “what does this compute?” over a max/min loop is a staple, and wrong initialization is the classic distractor.
← 3.8 Traversal