This module can read the simple representation of a sudoku found in the newspaper and display the solution. Ex:
~$ python sudoku.py
The problem:
6 7 4 3
9 6 2
5 3 4 6
7 4 1
8 9 3 4
1 5 7
2 6 3 5
3 2 8
4 5 7 2
A solution:
9 2 6 5 1 7 4 8 3
3 7 4 9 8 6 5 2 1
5 8 1 3 2 4 7 9 6
7 4 3 8 6 5 9 1 2
8 5 9 1 7 2 3 6 4
6 1 2 4 3 9 8 5 7
2 9 8 6 4 3 1 7 5
1 3 7 2 5 8 6 4 9
4 6 5 7 9 1 2 3 8
As a human brain usually scans the sudoku board and eventually deduces the right digit in the slots, the algorithm presented below operates in a different way. The algorithm below starts from the top left slot and stacks up assumptions for which digit is put in a slot, for each slot all the way to the bottom right slot. Eventually backtrack to a previous slot to try a different assumption when no number can be set for the current slot. On the contrary to the human brain, the algorithm does not do deductions but can easily remember a full stack of assumptions, which is something hard to do for the human brain.
The sudoku module offers three objects building a sudoku solver:
The Sudoku board class has the methods for reading the start state of a sudoku board, for representing a board. It also has the methods for setting and freeing a digit in a slot of the board, according to the rules of the sudoku game.
A 9x9 matrix which contains the digits on the sudoku board. An empty position is represented by a False value
Returns the list of possible values for the slot specified by the arguments, according to the current state of the sudoku board and according to the rules of the sudoku game.
The sudoku rules states that the candidates are the numbers which are not present neither in the column col, neither in the line row, neither in the square identified by col and row.
Takes a list of generators. This list is assumed to manipulate a shared representation of the problem. When this algorithm yields, a solution has been found and can be printed.
The algorithm works by calling the generator at the nth position of the list, and pulls the next() method on the iterator returned:
This algorithm yields whenever every generator of the list has yielded, at this point, every position of the board is filled with a digit according to the sudoku rules: a solution has been reached and the board can be printed.
When a generator has yielded, this means that a suitable candidate could be found and was set in the board’s slot and that an assumption can be tried on the next slot, with generator i+1.
When a generator raises a StopIteration, then a dead-end was met. A wrong assumption must have been taken somewhere along the stack of the previous recursion: the algorithm backtracks at the previous recursion, another assumption can be attempted.
It is interesting to note that the backtracking algorithm (the algorithm whichis able to stack assumptions) really is independant from the sudoku rules. The algorithm requires an argument with a precise interface and blindly triggers them, whether the argument is manipulating a sudoku, the eight queens or the knight problem. When the algorithm yields, the sudoku board or chessboard manipulated by the argument has cooked a solution.
Returns a list of candidate generators for use with the backtrack algorithm stack_assumptions. The sudoku argument must provide two functions: candidates(i,j), and attempt(col, row, candidate) and a member attribute called board, which is a 9x9 matrix.
There are as many generator functions than there are slots on the sudoku board, they are stored in a list. Each generator function is specific to a slot: it actually contains the coordinates of the slot, like a closure.
When called for the first time, the generator computes the list of candidate numbers for the slot, according to the current sudoku board. The list of candidates depends on the state of the board at the time the generator is called for the first time.