Lesson plan

Coding puzzles with Python lesson plan

Crack the classic programming puzzles as a space station crew, from FizzBuzz and primes to binary search, tic-tac-toe and calendar math, and finish with a Sudoku referee that lights up on the Pi.

4 chapters, 7 lessons, about 5 hours, on the Raspberry Pi 4 Model B. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • Remainders, rules as data, and the primes hiding in a signal.
  • Palindromes from both ends, and the halving trick behind binary search.
  • A board in a list of lists, a computer that blocks, and days counted from 1900.
  • Every idea in one referee: the Sudoku checker.

Materials for the whole unit

  • Half breadboard
  • LED x 2
  • Raspberry Pi 4 Model B
  • Resistor x 2

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 20 min

Number puzzles

Remainders, rules as data, and the primes hiding in a signal.

LessonTimeWhat students do
FizzBuzz and friendsThe most famous warm-up puzzle in programming: find the bug in a first FizzBuzz, fix the order of the tests, count a hundred answers, turn the rules into data so a new rule is one line, meet the hailstone numbers, and play it on two LEDs.40 min
  1. Run it: find the bug
  2. Order matters
  3. Count a hundred
  4. Rules as data
  5. A friend: hailstones
  6. FizzBuzz in lights
PrimesDecode a signal made of primes: test a number by trial division, stop at the square root, cross out multiples with the Sieve of Eratosthenes to find 1229 primes at once, hunt twin primes, split numbers into prime factors, and build a prime detector with two LEDs.40 min
  1. Run it: test every divisor
  2. Stop at the square root
  3. The sieve
  4. Twin primes
  5. Prime factors
  6. A prime detector

Materials

  • Half breadboard
  • LED x 2
  • Raspberry Pi 4 Model B
  • Resistor x 2

Check for understanding

Each student finishes "Primes" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "A prime detector".

Chapter 2. About 1 h 20 min

Words and search

Palindromes from both ends, and the halving trick behind binary search.

LessonTimeWhat students do
PalindromesWords that read the same both ways: reverse a string without the slice MicroPython lacks, clean phrases down to their letters, check from both ends with two pointers, find palindrome years and count palindrome numbers, find the longest one hidden in a string, and run a docking computer with two LEDs.40 min
  1. Run it: backwards and forwards
  2. Phrases
  3. Two pointers
  4. Palindrome numbers
  5. The longest one inside
  6. The docking computer
Guessing strategyGuess a secret number and discover binary search: play by halving the range, write a robot that halves for you, measure its worst case and average over every secret, see why a million numbers take only 20 guesses, and use the same idea to find the square root of 2.40 min
  1. Run it: a guessing game
  2. Halve it
  3. A robot player
  4. Worst case and average
  5. A million numbers
  6. Halving finds roots

Materials

  • Half breadboard
  • LED x 2
  • Raspberry Pi 4 Model B
  • Resistor x 2

Check for understanding

Each student finishes "Guessing strategy" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Halving finds roots".

Chapter 3. About 1 h 20 min

Grids and dates

A board in a list of lists, a computer that blocks, and days counted from 1900.

LessonTimeWhat students do
Grids and tic-tac-toeStore a grid as a list of lists: draw it, read cells by row and column, list all 8 winning lines to find a winner, play two players over the Serial Monitor with every bad move answered, light the winner on the LEDs, and play a computer that wins when it can and blocks when it must.40 min
  1. Run it: a grid in a list
  2. Rows and columns
  3. Find the winner
  4. Two players
  5. Lights for the winner, and a draw
  6. A computer that blocks
Calendar mathDate puzzles from mission control: fix a leap year rule that fails on 1900, count the day of the year, work out the weekday of the Moon landing by counting days from 1900, check your answer against datetime, and find the next Friday the 13th.40 min
  1. Run it: leap years
  2. Fix the rule
  3. Day of the year
  4. What day was the Moon landing?
  5. Check against datetime
  6. Friday the 13th

Materials

  • Half breadboard
  • LED x 2
  • Raspberry Pi 4 Model B
  • Resistor x 2

Check for understanding

Each student finishes "Calendar math" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Friday the 13th".

Chapter 4. About 50 min

Boss

Every idea in one referee: the Sudoku checker.

LessonTimeWhat students do
Boss: a Sudoku checkerThe finale puzzle: check a Sudoku grid. Draw it with box lines, check rows, columns and 3 x 3 boxes with one shared test, report exactly which digit repeats where, check a half-solved puzzle with zeros for blanks and light the answer, then take typed moves and judge each one.50 min
  1. Run it: the grid
  2. One test for every group
  3. Columns
  4. Boxes, and the verdict
  5. Say exactly what is wrong
  6. A puzzle in progress
  7. Referee live moves

Materials

  • Half breadboard
  • LED x 2
  • Raspberry Pi 4 Model B
  • Resistor x 2

Check for understanding

Each student finishes "Boss: a Sudoku checker" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Referee live moves".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.