Skip to main content

Sudoku solver with step-by-step guidance

Project description

Sudoku solver with step-by-step guidance

Installation

pip install dokusan

Quickstart

Sudoku Solver

The following code displays all steps leading to solution:

from dokusan import entities, techniques


class Unsolvable(Exception):
    pass


def list_steps(sudoku: entities.Sudoku):
    all_techniques = (
        techniques.PencilMarking,
        techniques.LoneSingle,
        techniques.HiddenSingle,
        techniques.NakedPair,
        techniques.NakedTriplet,
        techniques.LockedCandidate,
        techniques.XYWing,
        techniques.UniqueRectangle,
    )
    while not sudoku.is_solved():
        for technique in all_techniques:
            try:
                result = technique(sudoku).first()
            except techniques.NotFound as exc:
                continue
            else:
                sudoku.update(result.changes)
                yield result
                break
        else:
            raise Unsolvable

_ = 0

sudoku = entities.Sudoku.from_list([
    [_, _, _, _, 9, _, 1, _, _],
    [_, _, _, _, _, 2, 3, _, _],
    [_, _, 7, _, _, 1, 8, 2, 5],
    [6, _, 4, _, 3, 8, 9, _, _],
    [8, 1, _, _, _, _, _, _, _],
    [_, _, 9, _, _, _, _, _, 8],
    [1, 7, _, _, _, _, 6, _, _],
    [9, _, _, _, 1, _, 7, 4, 3],
    [4, _, 3, _, 6, _, _, _, 1],
])

for step in list_steps(sudoku):
    print(step.combination)

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.generator import generate


generate(min_rank=150)

Ranking and Sudoku difficulty

min_rank option is used to roughly estimate the difficulty of the sudoku. Sudoku with rank lower than 100 contains only naked/hidden singles. Sudoku with rank greater than 150 might 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 min_rank, so to ensure sudoku has desired rank one can do the following:

from dokusan import stats
from dokusan.generator import generate

min_rank = 450
while stats.rank(sudoku := generate(min_rank=min_rank)) < min_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.0a2.tar.gz (20.7 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.0a2-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dokusan-0.1.0a2.tar.gz
  • Upload date:
  • Size: 20.7 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.0a2.tar.gz
Algorithm Hash digest
SHA256 81b0a9d3a6c616e118dbc2150718d5b86f877585266658971e73acf332cd120c
MD5 a4740deafd9aa349c7b6a0ee36678982
BLAKE2b-256 456c76595243d71036d2ade780c13f183ee0963a241f833866ab1fb7a83516b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dokusan-0.1.0a2-py3-none-any.whl
  • Upload date:
  • Size: 21.5 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.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 69be6455ef3b6e38d368da7ec1d28423e12a87948967cb33884d71d754bf87e6
MD5 faef49b4af3962097f532566085eb365
BLAKE2b-256 b766e05477a61d56dda88eafa4836f13caec87530127cc8d1755036602c721e6

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