data_structures.arrays.sudoku_solver

Please do not modify this file! It is published at https://norvig.com/sudoku.html with only minimal changes to work with modern versions of Python. If you have improvements, please make them in a separate file.

Attributes

cols

digits

grid1

grid2

hard1

peers

rows

squares

start

unitlist

units

Functions

assign(values, s, d)

Eliminate all the other values (except d) from values[s] and propagate.

cross(items_a, items_b)

Cross product of elements in A and elements in B.

display(values)

Display these values as a 2-D grid.

eliminate(values, s, d)

Eliminate d from values[s]; propagate when values or places <= 2.

from_file(filename[, sep])

Parse a file into a list of strings, separated by sep.

grid_values(grid)

Convert grid into a dict of {square: char} with '0' or '.' for empties.

parse_grid(grid)

Convert grid to a dict of possible values, {square: digits}, or

random_puzzle([assignments])

Make a random puzzle with N or more assignments. Restart on contradictions.

search(values)

Using depth-first search and propagation, try all possible values.

shuffled(seq)

Return a randomly shuffled copy of the input sequence.

solve(grid)

solve_all(grids[, name, showif])

Attempt to solve a sequence of grids. Report results.

solved(values)

A puzzle is solved if each unit is a permutation of the digits 1 to 9.

some(seq)

Return some element of seq that is true.

test()

A set of unit tests.

Module Contents

data_structures.arrays.sudoku_solver.assign(values, s, d)

Eliminate all the other values (except d) from values[s] and propagate. Return values, except return False if a contradiction is detected.

data_structures.arrays.sudoku_solver.cross(items_a, items_b)

Cross product of elements in A and elements in B.

data_structures.arrays.sudoku_solver.display(values)

Display these values as a 2-D grid.

data_structures.arrays.sudoku_solver.eliminate(values, s, d)

Eliminate d from values[s]; propagate when values or places <= 2. Return values, except return False if a contradiction is detected.

data_structures.arrays.sudoku_solver.from_file(filename, sep='\n')

Parse a file into a list of strings, separated by sep.

data_structures.arrays.sudoku_solver.grid_values(grid)

Convert grid into a dict of {square: char} with ‘0’ or ‘.’ for empties.

data_structures.arrays.sudoku_solver.parse_grid(grid)

Convert grid to a dict of possible values, {square: digits}, or return False if a contradiction is detected.

data_structures.arrays.sudoku_solver.random_puzzle(assignments=17)

Make a random puzzle with N or more assignments. Restart on contradictions. Note the resulting puzzle is not guaranteed to be solvable, but empirically about 99.8% of them are solvable. Some have multiple solutions.

data_structures.arrays.sudoku_solver.search(values)

Using depth-first search and propagation, try all possible values.

data_structures.arrays.sudoku_solver.shuffled(seq)

Return a randomly shuffled copy of the input sequence.

data_structures.arrays.sudoku_solver.solve(grid)
data_structures.arrays.sudoku_solver.solve_all(grids, name='', showif=0.0)

Attempt to solve a sequence of grids. Report results. When showif is a number of seconds, display puzzles that take longer. When showif is None, don’t display any puzzles.

data_structures.arrays.sudoku_solver.solved(values)

A puzzle is solved if each unit is a permutation of the digits 1 to 9.

data_structures.arrays.sudoku_solver.some(seq)

Return some element of seq that is true.

data_structures.arrays.sudoku_solver.test()

A set of unit tests.

data_structures.arrays.sudoku_solver.cols
data_structures.arrays.sudoku_solver.digits = '123456789'
data_structures.arrays.sudoku_solver.grid1 = '003020600900305001001806400008102900700000008006708200002609500800203009005010300'
data_structures.arrays.sudoku_solver.grid2 = '4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......'
data_structures.arrays.sudoku_solver.hard1 = '.....6....59.....82....8....45........3........6..3.54...325..6..................'
data_structures.arrays.sudoku_solver.peers
data_structures.arrays.sudoku_solver.rows = 'ABCDEFGHI'
data_structures.arrays.sudoku_solver.squares
data_structures.arrays.sudoku_solver.start
data_structures.arrays.sudoku_solver.unitlist
data_structures.arrays.sudoku_solver.units