Skip to main content

Sudoku Solver in pure Python with no dependencies

Project description

Build Status PyPi version PyPi downloads PyPi license Coveralls

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 solver, which is a brute force methodology, when the basic induction is not enough.


Install by calling:

pip install dlxsudoku


Tests can be run using pytest:

py.test tests

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


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" + \

s1 = Sudoku(sudoku_string_1)

s2 = Sudoku(sudoku_string_2)

Sudoku formatting

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

# Optional comment or metadata

or as a one-liner:


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


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.

Files for dlxsudoku, version 0.9.3
Filename, size File type Python version Upload date Hashes
Filename, size dlxsudoku-0.9.3-py2.py3-none-any.whl (11.6 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size dlxsudoku-0.9.3.tar.gz (14.8 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page