Skip to main content
Build Status Coverage Status Checked with mypy PyPI Package latest release PyPI Wheel Supported versions GPLv3 License

Sudoku generator and solver with a step-by-step guidance

Installation

pip install dokusan

Quickstart

Sudoku Solvers

Step-by-step solver

This solver tries to solve sudoku using human-like strategies. Currently following techniques are supported:

  • Naked/Hidden singles

  • Naked Pairs/Triplets

  • Locked Candidate

  • XY-Wing

  • Unique Rectangle

For example to see all techniques that sudoku has:

from dokusan import solvers
from dokusan.boards import BoxSize, Sudoku


sudoku = Sudoku.from_list(
    [
        [0, 0, 0, 0, 9, 0, 1, 0, 0],
        [0, 0, 0, 0, 0, 2, 3, 0, 0],
        [0, 0, 7, 0, 0, 1, 8, 2, 5],
        [6, 0, 4, 0, 3, 8, 9, 0, 0],
        [8, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 9, 0, 0, 0, 0, 0, 8],
        [1, 7, 0, 0, 0, 0, 6, 0, 0],
        [9, 0, 0, 0, 1, 0, 7, 4, 3],
        [4, 0, 3, 0, 6, 0, 0, 0, 1],
    ],
    box_size=BoxSize(3, 3),
)

{step.combination.name for step in solvers.steps(sudoku)}

Backtracking-based solver

This solver is based on backtracking algorithm, however slightly modified to work fast

from dokusan import solvers, renderers
from dokusan.boards import BoxSize, Sudoku


sudoku = Sudoku.from_list(
    [
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 3, 0, 8, 5],
        [0, 0, 1, 0, 2, 0, 0, 0, 0],
        [0, 0, 0, 5, 0, 7, 0, 0, 0],
        [0, 0, 4, 0, 0, 0, 1, 0, 0],
        [0, 9, 0, 0, 0, 0, 0, 0, 0],
        [5, 0, 0, 0, 0, 0, 0, 7, 3],
        [0, 0, 2, 0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 4, 0, 0, 0, 9],
    ],
    box_size=BoxSize(3, 3),
)

solution = solvers.backtrack(sudoku)
print(renderers.colorful(solution))

Sudoku Generator

Generator algorithm is mainly based on article by Daniel Beer. The average time to generate Sudoku with rank of 150 is 700ms.

To generate a new sudoku:

from dokusan import generators, renderers


sudoku = generators.random_sudoku(avg_rank=150)
print(renderers.colorful(sudoku))

Ranking and Sudoku difficulty

avg_rank option roughly defines the difficulty of the sudoku. Sudoku with rank lower than 100 contains only naked/hidden singles. Sudoku with rank greater than 150 contains Naked Subsets/Locked Candidate/XY Wing/etc…, however this is not always guaranteed.

For higher ranks it is also not guaranteed that generated Sudoku rank will be higher than provided avg_rank, so to ensure sudoku has desired rank one can do the following:

from dokusan import generators, stats


avg_rank = 450
while stats.rank(sudoku := generators.random_sudoku(avg_rank)) < avg_rank:
    continue

Metadata

Release files for dokusan 0.1.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 dokusan 0.1.0
File Size Uploaded Provenance Actions
dokusan-0.1.0.tar.gz 21.5 kB Without provenance Details for dokusan-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dokusan 0.1.0
File Interpreter ABI Platform Actions
dokusan-0.1.0-py3-none-any.whl Python 3 none any Details for dokusan-0.1.0-py3-none-any.whl

Total release size: 44.2 kB

Release files / dokusan-0.1.0.tar.gz

Download URL dokusan-0.1.0.tar.gz
Size 21.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b504846e560e9fcddf99ed8bd38dab86f8d99a0c5f270cd2bea2c79347a6cd8b
BLAKE2b-256 checksum
How to use checksums
539b0c8f048f30d5bbf822c3337d4cd5ed8f75607f03588fdb4cade8cddcd162
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.8.16 Linux/5.15.0-1035-azure

Release files / dokusan-0.1.0-py3-none-any.whl

Download URL dokusan-0.1.0-py3-none-any.whl
Size 22.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cddb3456de83e1af430191fcb7e8f25b86eb1a36103f535de01c0c0331f5baaa
BLAKE2b-256 checksum
How to use checksums
63aeb3ed699da71ac18d2d578725980d87be1b4fbc973cfa548e4c0968c2efd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.8.16 Linux/5.15.0-1035-azure

Security for dokusan 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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