Skip to main content

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

Project description

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.entities 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
from dokusan.entities 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),
)

solvers.backtrack(sudoku)

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


generators.random_sudoku(avg_rank=150)

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

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

dokusan-0.1.0a5.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dokusan-0.1.0a5-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

Details for the file dokusan-0.1.0a5.tar.gz.

File metadata

  • Download URL: dokusan-0.1.0a5.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.8.0 Linux/5.0.0-1027-azure

File hashes

Hashes for dokusan-0.1.0a5.tar.gz
Algorithm Hash digest
SHA256 c0b080857fb8d1ef0338756fd629182e89d65cdb5ac2c88e08b7ae6a22edebc5
MD5 0b4260f44e645c14746ec76dc8da70af
BLAKE2b-256 c0868d35cc84275c3470ebb065c98a9c2a6a13fea4579228e94e505a5234eeb9

See more details on using hashes here.

File details

Details for the file dokusan-0.1.0a5-py3-none-any.whl.

File metadata

  • Download URL: dokusan-0.1.0a5-py3-none-any.whl
  • Upload date:
  • Size: 22.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.8.0 Linux/5.0.0-1027-azure

File hashes

Hashes for dokusan-0.1.0a5-py3-none-any.whl
Algorithm Hash digest
SHA256 f898ecdb00adef2c029fea77e876392b0a5863174447fc1e436020ef7f30a8f8
MD5 55df4310a4f9e72984bd694f766ae002
BLAKE2b-256 69aece4971a6f336ad691174fb63d297bf652ed06dc6888174bc751ed5f4536b

See more details on using hashes here.

Supported by

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