Lessons and the learning game

Coding puzzles with Python

The classic programming puzzles, from FizzBuzz to a Sudoku checker, cracked by a space station crew on a Raspberry Pi 4.

The course

Coding puzzles with Python is a beginner Coding course in seven lessons, for anyone who has just finished Python from zero. Each lesson takes one famous puzzle from a first answer to a good one, and says why the good one is better: less work, fewer special cases, or data instead of code. It runs on the gallery's two-LED Pi build, yellow on GPIO27 for yes and red on GPIO17 for no, and every step is checked against what the Pi prints or what its pins do.

  1. FizzBuzz and friends: the bug where 15 says Fizz, the most specific test first, a hundred answers counted (27, 14, 6 and 53), the rules as a list so a new rule is one line (105 is FizzBuzzBang), hailstone numbers (97 takes 118 steps), and the game on two LEDs with no if at all.
  2. Primes: trial division, stopping at the square root (99 tries for 9973 instead of 9971), the Sieve of Eratosthenes (1,229 primes below 10,000), 35 twin prime pairs below 1,000, prime factors, and a prime detector you type numbers into.
  3. Palindromes: reversing with reversed(), phrases cleaned to their letters, two pointers that reject "rocket" after one comparison, the next palindrome year (2112) and 198 palindromes up to 9999, the longest palindrome hidden in a string, and a docking computer.
  4. Guessing strategy: a guessing game won by halving, a robot that plays binary search, a worst case of 7 and an average of 5.8 over every secret, 20 guesses for a million numbers, and the square root of 2 by bisection.
  5. Grids and tic-tac-toe: a board as a list of lists, cells by row and column, all 8 winning lines, a two-player game over the Serial Monitor with every bad move answered, lights for the winner and a draw, and a computer that wins when it can and blocks when it must.
  6. Calendar math: the leap year rule that 1900 breaks, the day of the year (November 11, 2026 is day 315), the Moon landing's weekday counted from 1900 (a Sunday), Sputnik to the Moon in 4,307 days checked against datetime, and the next Friday the 13th.
  7. Boss: a Sudoku checker: the finale.

The finale

The boss is a Sudoku referee. It draws the grid with box lines, checks rows, columns and 3 x 3 boxes with one shared test, and gives a verdict. On a broken grid it says exactly what is wrong (row 4, box 6 and column 7 each have a 5 twice, which points at one square). It checks a puzzle still being solved, with zeros for blanks, and lights yellow when nothing clashes. Then it takes typed moves, row, column and digit, and judges each one live, red for a clash. The next step, a solver, is the finale of Recursion and backtracking.

What the Pi engine does differently

The Pi runs MicroPython 1.29, not CPython, and puzzles meet its edges:

  • Slices with a step are not supported: word[::-1] raises NotImplementedError, so strings are reversed with "".join(reversed(word)). Plain slices like word[1:4] work.
  • str.ljust, str.rjust and str.zfill are missing; f-string formats such as {month:02d} do the padding.
  • A dict does not keep the order keys went in, so counts are printed by name rather than by printing the dict.
  • The random generator differs from CPython's, so the guessing game's seeded secret is not the same number on a real Pi.

datetime, calendar, big integers and gpiozero's LED work as they do on a real Pi.