Examples¶
Sudoku¶
Sudoku example
You can run this module using:
$ python3 -m pyswip.examples.sudoku
- class pyswip.examples.sudoku.Matrix(matrix)¶
Represents a 9x9 Sudoku puzzle
- classmethod from_text(text)¶
Create a Matrix from the given string
The following are valid characters in the string:
.: Blank column
1-9: Numbers
The text must contain exactly 9 rows and 9 columns. Each row ends with a newline character. You can use blank lines and spaces/tabs between columns.
- Parameters:
text (
str) – The text to use for creating the Matrix- Return type:
>>> puzzle = Matrix.from_text(''' ... . . 5 . 7 . 2 6 8 ... . . 4 . . 2 . . . ... . . 1 . 9 . . . . ... . 8 . . . . 1 . . ... . 2 . 9 . . . 7 . ... . . 6 . . . . 3 . ... . . 2 . 4 . 7 . . ... . . . 5 . . 9 . . ... 9 5 7 . 3 . . . . ... ''')
- pretty_print(*, file=None)¶
Prints the matrix as a grid
- Parameters:
file (
Optional[IO]) – The file to use for printing- Return type:
None
>>> import sys >>> puzzle = sample_puzzle() >>> puzzle.pretty_print(file=sys.stdout) . . 5 . 7 . 2 6 8 . . 4 . . 2 . . . . . 1 . 9 . . . . . 8 . . . . 1 . . . 2 . 9 . . . 7 . . . 6 . . . . 3 . . . 2 . 4 . 7 . . . . . 5 . . 9 . . 9 5 7 . 3 . . . .
- pyswip.examples.sudoku.prolog_source()¶
Returns the Prolog source file that solves Sudoku puzzles.
- Return type:
str
- pyswip.examples.sudoku.solve(matrix)¶
Solves the given Sudoku puzzle
- Parameters:
matrix (
Matrix) – The matrix that contains the Sudoku puzzle- Return type:
Union[Matrix,Literal[False]]
>>> puzzle = sample_puzzle() >>> print(puzzle) . . 5 . 7 . 2 6 8 . . 4 . . 2 . . . . . 1 . 9 . . . . . 8 . . . . 1 . . . 2 . 9 . . . 7 . . . 6 . . . . 3 . . . 2 . 4 . 7 . . . . . 5 . . 9 . . 9 5 7 . 3 . . . . >>> print(solve(puzzle)) 3 9 5 4 7 1 2 6 8 8 7 4 6 5 2 3 9 1 2 6 1 3 9 8 5 4 7 5 8 9 7 6 3 1 2 4 1 2 3 9 8 4 6 7 5 7 4 6 2 1 5 8 3 9 6 1 2 8 4 9 7 5 3 4 3 8 5 2 7 9 1 6 9 5 7 1 3 6 4 8 2
Hanoi¶
- pyswip.examples.hanoi.solve(disk_count=3, simple=False, file=None)¶
Solves the Towers of Hanoi problem.
- Parameters:
disk_count (
int) – Number of disks to usesimple (
bool) – If set toTrue, only the moves are printed. Otherwise all states are drawn.file (
IO) – The file-like object to output the steps of the solution. By default stdout is used.
- Return type:
None
>>> solve(3, simple=True) 1. Move disk from left pole to right pole. 2. Move disk from left pole to center pole. 3. Move disk from right pole to center pole. 4. Move disk from left pole to right pole. 5. Move disk from center pole to left pole. 6. Move disk from center pole to right pole. 7. Move disk from left pole to right pole.
- pyswip.examples.hanoi.prolog_source()¶
Returns the Prolog source file that solves the Towers of Hanoi problem.
- Return type:
str
Coins¶
- pyswip.examples.coins.solve(*, coin_count=100, total_cents=500, max_solutions=1)¶
Solves the coins problem.
Finds and returns combinations of
coin_countcoins that makestotalcents.- Parameters:
coin_count (
int) – Number of coinstotal_cents (
int) – Total cent value of coins
- Return type:
List[Dict[int,int]]
- pyswip.examples.coins.prolog_source()¶
Returns the Prolog source file that solves the coins problem.
- Return type:
str