module special.sudoku
#
Short summary#
module ensae_teaching_cs.special.sudoku
This file proposes a simple algorithm to solve a Sudoku. It finds the first possible solution.
Functions#
function |
truncated documentation |
---|---|
Look into every number in sub square i, j, if a number in it |
|
Look into every number in column j, if the number in column k |
|
Look into every number in line i, if the number in column k |
|
look over all empty place and pick the one with the least possible options |
|
tells for a particular position the list of possible number |
|
Solves the Sudoku. |
|
Converts a sudoku into a string. |
Documentation#
This file proposes a simple algorithm to solve a Sudoku. It finds the first possible solution.
- ensae_teaching_cs.special.sudoku.chiffre_barre_carre(s, r, i, j)#
Look into every number in sub square i, j, if a number in it
s[i][k]
is not null,- Paramètres:
s – current state of the sudoku
r – array
r[n] == 0
means number n+1 is already taken on this sub squarei – sub square are indexed by
(0 based)
j – see parameter i
- ensae_teaching_cs.special.sudoku.chiffre_barre_colonne(s, r, j)#
Look into every number in column j, if the number in column k
s[i][k]
is not null,- Paramètres:
s – current state of the sudoku
r – array
r[n] == 0
means number n+1 is already taken on this columnj – column index (0 based)
- ensae_teaching_cs.special.sudoku.chiffre_barre_ligne(s, r, i)#
Look into every number in line i, if the number in column k
s[i][k]
is not null,- Paramètres:
s – current state of the sudoku
r – array
r[n] == 0
means number n+1 is already taken on this linei – line index (0 based)
- ensae_teaching_cs.special.sudoku.meilleure_case(s)#
look over all empty place and pick the one with the least possible options
- Paramètres:
s – current state of the sudoku
- Renvoie:
(i,j,r), (i,j) is the best position, r the list of possible numbers
- ensae_teaching_cs.special.sudoku.nombre_possible(s, i, j)#
tells for a particular position the list of possible number
- Paramètres:
s – current state of the sudoku
i – line index (0 based)
j – column index (0 based)
- Renvoie:
9-element array, 0: not possible, 1: possible at position (i, j)
- ensae_teaching_cs.special.sudoku.resolution_sudoku(s)#
Solves the Sudoku.
- Paramètres:
s – sudoku (0 for empty case)
- Renvoie:
0 for impossible, 1 for possible, then s contains the solution
The algorithm is the following:
Find the position of a zero element (no number yet) with the smallest number of options
If there is at least one possible option, try the first one, go to step 1
If not, the Sudoku cannot be solved, go back to last list of multiple options and try the next one.
Example:
s = [[0, 0, 0, 3, 0, 0, 8, 0, 0], [0, 0, 7, 9, 0, 8, 0, 0, 5], [0, 0, 0, 2, 0, 4, 1, 0, 0], [0, 9, 0, 8, 1, 0, 0, 4, 7], [0, 4, 0, 0, 0, 0, 0, 0, 6], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 5, 0, 2, 0], [5, 3, 4, 0, 0, 0, 0, 0, 0], [0, 0, 0, 7, 0, 0, 0, 0, 0]] resolution_sudoku(s) for i in range(0, 9): print(s[i])
- ensae_teaching_cs.special.sudoku.sudoku2str(su)#
Converts a sudoku into a string.
- Paramètres:
su – sudoku
- Renvoie:
string