Skip to main content

Build Status PyPi version PyPi downloads PyPi license Coverage Status

Sudoku Solver written in pure Python with no dependencies.

It solves Sudokus of sizes N x N by pure induction as far as is possible, and then uses an optional Dancing Links brute force solver, when the basic induction is not enough.

Installation

Install with pip:

pip install dlxsudoku

Testing

Tests can be run using pytest:

py.test tests

The tests make a HTTP request to a file containing several Sudokus on Project Euler.

Usage

A Sudoku stored in a file can be solved as such:

from dlxsudoku import Sudoku

s = Sudoku.load_file('path/to/sudoku.sud')
s.solve(verbose=True, allow_brute_force=True)

Alternatively, if your Sudoku is stored in string variable it can be solved in the following fashion:

from dlxsudoku import Sudoku

sudoku_string_1 = "030467050920010006067300148301006027400850600090200400005624001203000504040030702"
sudoku_string_2 = "# Example Sudoku\n" + \
                  "*72****6*\n" + \
                  "***72*9*4\n" + \
                  "*9*1****2\n" + \
                  "*******4*\n" + \
                  "82*4*71**\n" + \
                  "**9*6*8**\n" + \
                  "***9**6**\n" + \
                  "**3*72*9*\n" + \
                  "*6*843*7*"

s1 = Sudoku(sudoku_string_1)
s1.solve()
print(s1.to_oneliner())

s2 = Sudoku(sudoku_string_2)
s2.solve()
print(s2)

DLXSudoko treats a Sudoku with multiple solutions as a faulty one and raises a dlxsudoku.exceptions.SudokuHasMultipleSolutionsError exception in such a situation.

Use from terminal

DLXSudoku also installs a console entry point. Can solve Sudokus from string or from path:

solve-sudoku --sudoku 030467050920010006067300148301006027400850600090200400005624001203000504040030702

or

solve-sudoku --path "path/to/sudoku.sud"

Sudoku formatting

A Sudoku file or string should be structured in the following manner:

# Optional comment or metadata
*72****6*
***72*9*4
*9*1****2
*******4*
82*4*71**
**9*6*8**
***9**6**
**3*72*9*
*6*843*7*

or as a one-liner:

030467050920010006067300148301006027400850600090200400005624001203000504040030702

Any character other than [1-9] may be used as a placeholder for unknowns.

References

The Dancing Links code has been adapted from Algorithm X in 30 lines!, only modified slightly to accommodate class structure and Python 2.6.

Release files for dlxsudoku 0.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dlxsudoku 0.10.0
File Size Uploaded
dlxsudoku-0.10.0.tar.gz 15.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dlxsudoku 0.10.0
File Interpreter ABI Platform
dlxsudoku-0.10.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 27.1 kB

Release files / dlxsudoku-0.10.0.tar.gz

Download URL dlxsudoku-0.10.0.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
07862e94289b7e4f8f27c1d57f999b4f9b54dc49c8cdf5280976130fe72690b4
BLAKE2b-256 checksum
How to use checksums
719bc0794fae949360c431e55d43b4973aee4d4c98a98dc347e108dc38750534
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / dlxsudoku-0.10.0-py2.py3-none-any.whl

Download URL dlxsudoku-0.10.0-py2.py3-none-any.whl
Size 12.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b2671b717e0347b968fc33ffdd9b714bb7bd01e35d61df2a41f9a545d740ae52
BLAKE2b-256 checksum
How to use checksums
7bcd231df26b620319427d25ec2becd47e23b12bb20c4258dd677cb4eaf99d42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.10.3

2 release files

0.10.2

2 release files

This release

0.10.0 This release

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page