Skip to main content

Sudoku Solver in pure Python with no dependencies

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dlxsudoku-0.10.2.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

dlxsudoku-0.10.2-py2.py3-none-any.whl (12.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file dlxsudoku-0.10.2.tar.gz.

File metadata

  • Download URL: dlxsudoku-0.10.2.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for dlxsudoku-0.10.2.tar.gz
Algorithm Hash digest
SHA256 d4962da12036a0cd82178f01136f227f482fb226fac5321b78f3a405c68577a5
MD5 922296a80bb61b3869a336684618b65e
BLAKE2b-256 4b8469e0c1eaca56433ceacb7a7449e23f977a57bed07c536e92787723980587

See more details on using hashes here.

File details

Details for the file dlxsudoku-0.10.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for dlxsudoku-0.10.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d174962cd8d3d159117b3e6be846e2beeacb729a7dea260cbae62167be020872
MD5 9968535210711a80c806ca00e67758c5
BLAKE2b-256 3708d2a8b366da83c13565871bbf3ae2ee612a67a72e43a0f52af686e15f7d77

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page