Basis Lab

The Patterns Behind Most Coding Problems

Choosing the Pattern From the Words of the Problem

Last timeSearching the Answer Instead of the Input

A decision procedure that reads the pattern off what a statement says about its input, plus the two cases where every pattern here is the wrong answer.

Six mechanisms have been derived, each from a piece of structure in an input.

What has not been said is how to pick one in the three minutes before the

writing starts, from a statement nobody has labelled, with nothing to recognise

and nothing to recall. That decision is the whole skill, and it is almost never

taught, because it is the part that looks like judgement.

It is not judgement. It is a short list of questions asked about the input in a

fixed order, where the first one that matches decides and nothing after it is

read. Two of those questions rule the whole thing out, one of them is pure

arithmetic on the stated bound, and the rest are one structure each.

The input sentence decides, not the question

Nearly every statement is two halves. A sentence or two describing what you are

given, and a sentence saying what to produce. The instinct is to read the

second half first, and the second half is almost useless for choosing, because

the verbs in it are shared by every pattern here. Find appears in all of them.

So do count, minimise, and how many. A verb tells you what to accumulate inside

the loop and nothing about which loop to write.

The deciding facts are all in the first half. Does the input arrive in some

order. Does the question concern neighbours, or any items at all. Is only part

of the answer wanted. What bound is given on the size. Four facts, each a

phrase long, and between them they pick the method.

A quick way to feel this is to take two statements with the same verb. Find two

items summing to a target in a sorted list, and find two items summing to a

target in a list in arbitrary order. The verb is identical and the methods are

different patterns from different lessons. Now take two with different verbs

and the same input sentence. The longest run of neighbours whose total is under

a limit, and the count of runs of neighbours whose total is under a limit. The

verbs differ and both are a sliding window with a different line inside it.

The budget is the first question, and it is arithmetic

A statement that says n≤1000n \le 1000 has told you what method it wants. At

roughly a hundred million simple steps in a second, which is the order of

magnitude a modern machine manages for arithmetic on values already in cache,

a thousand items gives the double loop 1000×999/2=499,5001000 \times 999 / 2 = 499{,}500 pairs.

That is five thousandths of the budget. There is nothing to optimise and

nothing to be clever about.

The lesson stops here

5 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01What a Pattern Is Actually Exploiting, and Why It Is Never Cleverness
  2. 02Two Ends Moving Inward, and the Proof That Nothing Is Lostopening only
  3. 03A Window That Slides, and Why Its Work Stays Linearopening only
  4. 04Remembering What You Have Seen, and What the Memory Costsopening only
  5. 05Totals You Compute Once, Then Read by Subtractionopening only
  6. 06Keeping Only the Best Few Without Sorting the Restopening only
  7. 07Searching the Answer Instead of the Inputopening only
  8. 08Choosing the Pattern From the Words of the Problemyou are here

Read alongside