Member-only story
I recently came across Peter Norvig’s Solving Every Sudoku Puzzle. I was impressed with his concise and beautiful Python code that solves any Sudoku puzzles systematically.
However, some people may find it difficult to understand the concise code hence are unable to appreciate the beauty. I break down his article and explain the code in a step-by-step manner.
Note: the original article by Peter uses Python 2 but I’m using Python 3 in this article. In some places, I changed the indentation of Peter’s code but I did not alter his program logic at all.
The Rule of Sudoku
If you are not familiar with Sudoku, I recommend that you visit Sudoku Dragon and read their Sudoku rule description.
Peter gives a beautiful summary of its rule in one sentence.
A puzzle is solved if the squares in each unit are filled with a permutation of the digits 1 to 9.
This may not be very clear at first glance if you don’t know what squares and units are.
Squares
A Sudoku puzzle is a grid of 81 squares; the majority of enthusiasts label the columns 1–9, the rows A-I, and call a collection of nine squares (column, row, or box)
A1 A2 A3| A4 A5 A6| A7 A8 A9
B1 B2 B3| B4 B5 B6|…