cadence_learn
think_like_a_programmer / level 05 of 12 ▶ 12 min

the same problem in different clothes.

A classroom, an airline and a video game turn out to be one problem. Learn to see the shape and you stop meeting new problems — you start recognising old ones.

after this level you'll be able to
  • Spot when a new problem is one you have already solved
  • Rewrite a problem with the specifics removed so the shape shows
  • Notice when a familiar shape almost fits but does not quite
the whole idea

you will meet the same problem wearing different clothes

the one sentence

Problems that sound completely unrelated are usually the same problem underneath — and once you can see the shape, you already know how to solve every problem that has it.

three problems, one shape
the tallest student a classroom the cheapest flight an airline the highest score a video game TAKE THE NOUNS OUT ONE SHAPE 1 · take the first item as the best so far 2 · look at each of the others in turn 3 · if it beats the best so far, it becomes the best so far 4 · when none are left, the best so far is the answer
Learn it once, and you have solved all three — plus the shortest word, the earliest date and the closest star. Only step 3 changes: what “beats” means.

This is the skill that separates someone who has memorised twenty solutions from someone who can handle the twenty-first. The twenty-first is never new. It is one of the twenty in a different outfit.

Spotting the shape is called pattern recognition. Writing the shape down with the specifics stripped out is called abstraction. Those two words sound academic and they describe something you already do constantly — you have never been taught how to open this particular door, you learned doors.

try it yourself

three problems, one shape

Read the three below. They have nothing to do with each other — a classroom, an airline, a video game. Click a step on the left and watch the matching line light up in all three at once.

▤ lab 05 · the same shape

Then press the button underneath and watch the three become one.

the classroom
Write down the first student’s height as the tallest so far.
Go along the row, one student at a time.
If this student is taller than your tallest so far, write theirs down instead.
At the end of the row, whoever is written down is the tallest.
the airline
Note the first flight’s price as the cheapest so far.
Work down the list of flights, one at a time.
If this flight costs less than your cheapest so far, note this one instead.
At the bottom of the list, the noted flight is the cheapest.
the video game
Take the first score as the high score so far.
Go through the remaining scores one at a time.
If this score is higher than your high score so far, replace it.
Once every score is checked, the one you kept is the high score.
Click any of the four steps above. Watch all three problems light up on the same line.
◈ ask an ai about this

“What do pattern recognition and abstraction mean in computer science? Use an everyday example, not code.”

chatgpt ↗ claude ↗

the payoff

one shape, and you are done forever

What you just made is a shape with the specifics removed. It does not care about students, flights or scores — it cares about items and what “beats” means. Swap in a different meaning of beats and the same four steps find the shortest word, the earliest date, the closest star.

You will meet this exact shape in your course, probably in week four or five, and it will be called something like finding the maximum. People who did not do this level memorise it as a new fact. You already know it, and you know why the first line has to be there.

a real question worth sitting with

Step 1 says take the first one as the best so far. Why not start with zero, or with nothing? Try it: find the coldest temperature in a list where every temperature is below freezing. If you start from zero, your answer is zero — a temperature that is not even in the list. Starting from a real item is not a detail. It is the whole correctness of the thing.

◈ ask an ai about this

“Why is it wrong to start a “find the smallest” algorithm at zero? Show me a case where it gives the wrong answer.”

chatgpt ↗ claude ↗

do this together

hunt for the shape

☶ two people · 12 minutes

One of you invents problems. The other tries to break the shape.

person a — the inventor

Make up everyday “find the…” problems. The busiest day. The nearest coffee shop. The longest song. Whatever comes to mind.

person b — the matcher

For each one, say the four steps out loud with that problem’s words in them. Then try hard to invent one where the shape does not fit.

Person B has the better job, because the interesting ones are the near-misses. “Find the two tallest.” “Find the most common word.” Those need a changed shape, not this one — and noticing that a familiar shape almost fits, but not quite, is exactly the judgment this course is trying to build.

what to keep

three things worth remembering

01

New problems are rarely new

Before solving, ask what this reminds you of. It usually reminds you of something.

02

Strip the nouns out

Replace students and flights with “items” and the shape appears on its own.

03

Watch the near-misses

“Almost the same shape” is where the real thinking — and most of the marks — live.

check yourself

2 questions before you move on

Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.

▢ check yourself2 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

The tallest student, the cheapest flight and the highest score. How many different algorithms do you need?

02

Why must “find the smallest” start from a real item rather than from zero?

answered: 0 of 2right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →