Skip to content

structure

This module provides the two classes Tile and Sudoku that serve as base structure for the solver to operate on. Observe that the abbreviations 'r', 'c' and 's' for row, column and square, stored in CONTAINER_TYPES, are used throughout this program. Square, thereby, refers to typical 3x3 tile collections.

Sudoku #

Container structure to represent the Sudoku grid by storing 81 Tile objects in a one dimensional list.

containers: Dict[str, List[List[int]]] property #

Structure to store the indices of the tiles, i.e. their position in the tiles array, that live in each row, column and square.

Consider the following example to retrieve the array positions of the tiles that live in the first square:

Examples:

>>> Sudoku.containers['s'][0]
[0, 1, 2, 9, 10, 11, 18, 19, 20]

Thereby, the subscript ['s'][0] indicates the square at position 0.

Returns:

Type Description
Dict[str, List[List[int]]]

The containers (as described above)

done: bool property #

Returns: Whether the puzzle is solved

max_options: int property #

Returns:

Type Description
int

The maximum number of candidates over all the tiles

occurrences: Dict[str, List[List[Set[int]]]] property #

For each row, column and square and for each number of 1 to 9, this dictionary stores at which tiles positions the respective value still appears as candidate.

Consider the following example to retrieve the set of array positions at which the candidate 9 is still found in the second row:

Examples:

>>> Sudoku.occurrences['r'][1][8]
{9, 11, 15}

Thereby, ['r'][1] indicates the second row and [8] specifies the list position at which the occurrences of the value 9 are stored.

Returns:

Type Description
Dict[str, List[List[Set[int]]]]

The occurrences (as above duh xD)

tiles: List[Tile] property #

Returns:

Type Description
List[Tile]

List of length 81 to store every tile of the Sudoku grid.

get_complexity_map() #

Returns: The solving step a which the definite solution has been found for each tile (organized row by row).

get_options() #

Returns:

Type Description
List[List[set]]

The remaining candidates for each tile as set organized row by row

get_solved() #

Returns: The solved puzzle as a two-dimensional list.

get_tiles() #

Returns:

Type Description
List[List[Tile]]

The tiles organized row by row

is_valid() #

Explicitly check whether the Sudoku rules have been violated in the solving process. One should try to avoid explicit use of this method as the implementations of the solving algorithms should automatically detect if such a violation has been taken place and correspondingly set the violated attribute to True.

Returns:

Type Description
bool

Is the present configuration valid?

Tile #

Structure to represent a tile of the Sudoku grid. Tile objects store the position of a tile by means of specifying their row, column and square index. Moreover, this class is needed to keep track of the possible candidate values a tile can still take in the process of solving the puzzle.

options: set property writable #

Returns:

Type Description
set

The remaining candidate values for this tile

pos: Dict[str, int] property #

Get the row, column and square index of the tile formatted as {'r': *row index*, 'c': *column index*, 's': *square index*}.

Returns:

Type Description
Dict[str, int]

The row, column and square index of the tile

to_none_tile(tile) classmethod #

Create a tile that has no candidate values at the position of the tile that is given as argument.

index_to_pos(t) #

Get the row, column and square index from the tile index t formatted as {'r': *row index*, 'c': *column index*, 's': *square index*}.

Parameters:

Name Type Description Default
t int

The tile index

required

Returns:

Type Description
Dict[str, int]

Dict containing row, column and square index

row_column_to_index(r, c) #

Get the tile index, i.e. the position of the tile in an array of dimension 81, by specifying the row r and the column c.

Parameters:

Name Type Description Default
r int

Row index

required
c int

Column index

required

Returns:

Type Description
int

Tile index