Almost every problem you meet is one of ten shapes. Learn the shapes and problems stop feeling new.
Pattern 1, Counting. How many items satisfy a condition? Start a counter at zero, add one each time the condition holds.
EXAMPLE
loading editor…
YOUR TURN
Your turn → Counter starts outside, increments inside. Count the minors instead.
loading editor…
Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.
Pattern 2, Accumulation. Combine everything into one value: a sum, a product, a joined string. Same skeleton, different operation in the body.
EXAMPLE
loading editor…
YOUR TURN
Your turn → Change it to compute the product instead. Careful: what must the starting value be for multiplication?
loading editor…
Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.
Pattern 3, Searching. Find the first item matching a condition and stop. break is what makes searching efficient, there is no reason to keep looking after you have found it.
EXAMPLE
loading editor…
YOUR TURN
Your turn → Starting `found` at None/null means 'not found yet', and it stays that way if nothing matches. Search for a letter nobody has.
loading editor…
Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.
Mark this lesson complete
Signed in, your progress follows you to every device.