data_structures.arrays.sudoku_solver ==================================== .. py:module:: data_structures.arrays.sudoku_solver .. autoapi-nested-parse:: 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 ---------- .. autoapisummary:: data_structures.arrays.sudoku_solver.cols data_structures.arrays.sudoku_solver.digits data_structures.arrays.sudoku_solver.grid1 data_structures.arrays.sudoku_solver.grid2 data_structures.arrays.sudoku_solver.hard1 data_structures.arrays.sudoku_solver.peers data_structures.arrays.sudoku_solver.rows data_structures.arrays.sudoku_solver.squares data_structures.arrays.sudoku_solver.start data_structures.arrays.sudoku_solver.unitlist data_structures.arrays.sudoku_solver.units Functions --------- .. autoapisummary:: data_structures.arrays.sudoku_solver.assign data_structures.arrays.sudoku_solver.cross data_structures.arrays.sudoku_solver.display data_structures.arrays.sudoku_solver.eliminate data_structures.arrays.sudoku_solver.from_file data_structures.arrays.sudoku_solver.grid_values data_structures.arrays.sudoku_solver.parse_grid data_structures.arrays.sudoku_solver.random_puzzle data_structures.arrays.sudoku_solver.search data_structures.arrays.sudoku_solver.shuffled data_structures.arrays.sudoku_solver.solve data_structures.arrays.sudoku_solver.solve_all data_structures.arrays.sudoku_solver.solved data_structures.arrays.sudoku_solver.some data_structures.arrays.sudoku_solver.test Module Contents --------------- .. py:function:: 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. .. py:function:: cross(items_a, items_b) Cross product of elements in A and elements in B. .. py:function:: display(values) Display these values as a 2-D grid. .. py:function:: 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. .. py:function:: from_file(filename, sep='\n') Parse a file into a list of strings, separated by sep. .. py:function:: grid_values(grid) Convert grid into a dict of {square: char} with '0' or '.' for empties. .. py:function:: parse_grid(grid) Convert grid to a dict of possible values, {square: digits}, or return False if a contradiction is detected. .. py:function:: 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. .. py:function:: search(values) Using depth-first search and propagation, try all possible values. .. py:function:: shuffled(seq) Return a randomly shuffled copy of the input sequence. .. py:function:: solve(grid) Solve the grid. .. py:function:: 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. .. py:function:: solved(values) A puzzle is solved if each unit is a permutation of the digits 1 to 9. .. py:function:: some(seq) Return some element of seq that is true. .. py:function:: test() A set of unit tests. .. py:data:: cols :value: '123456789' .. py:data:: digits :value: '123456789' .. py:data:: grid1 :value: '003020600900305001001806400008102900700000008006708200002609500800203009005010300' .. py:data:: grid2 :value: '4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......' .. py:data:: hard1 :value: '.....6....59.....82....8....45........3........6..3.54...325..6..................' .. py:data:: peers .. py:data:: rows :value: 'ABCDEFGHI' .. py:data:: squares .. py:data:: start .. py:data:: unitlist .. py:data:: units