An open API service indexing awesome lists of open source software.

https://github.com/gmontamat/sudoku-solver

Sudoku solver implemented in Python 2.7
https://github.com/gmontamat/sudoku-solver

puzzle sudoku-solver

Last synced: about 2 months ago
JSON representation

Sudoku solver implemented in Python 2.7

Awesome Lists containing this project

README

        

# sudoku-solver

Sudoku solver implemented in Python 2.7

## Note

The solver works very well on any 9x9 puzzle but the main purpose of this repo is to improve the style of the original code and make it more readable.

## World's hardest Sudoku

The following puzzle was obtained [here](http://www.telegraph.co.uk/news/science/science-news/9359579/Worlds-hardest-sudoku-can-you-crack-it.html). It takes slightly less than 10 seconds to solve.

```python
import sudoku

sudoku_string = (
'8........'
'..36.....'
'.7..9.2..'
'.5...7...'
'....457..'
'...1...3.'
'..1....68'
'..85...1.'
'.9....4..'
)
hard_sudoku = sudoku.Sudoku(sudoku_string)
print hard_sudoku
```

```
8 ? ? | ? ? ? | ? ? ?
? ? 3 | 6 ? ? | ? ? ?
? 7 ? | ? 9 ? | 2 ? ?
------+-------+------
? 5 ? | ? ? 7 | ? ? ?
? ? ? | ? 4 5 | 7 ? ?
? ? ? | 1 ? ? | ? 3 ?
------+-------+------
? ? 1 | ? ? ? | ? 6 8
? ? 8 | 5 ? ? | ? 1 ?
? 9 ? | ? ? ? | 4 ? ?
```

```python
hard_sudoku.solve()
print hard_sudoku
```

```
8 1 2 | 7 5 3 | 6 4 9
9 4 3 | 6 8 2 | 1 7 5
6 7 5 | 4 9 1 | 2 8 3
------+-------+------
1 5 4 | 2 3 7 | 8 9 6
3 6 9 | 8 4 5 | 7 2 1
2 8 7 | 1 6 9 | 5 3 4
------+-------+------
5 2 1 | 9 7 4 | 3 6 8
4 3 8 | 5 2 6 | 9 1 7
7 9 6 | 3 1 8 | 4 5 2
```

## Unit testing

```
python2 test_sudoku.py
```

```
Speed test
----------
Easy runtime: 0.286250829697
Medium runtime: 0.0661108493805
Hard runtime: 9.63975000381

...
----------------------------------------------------------------------
Ran 3 tests in 0.035s

OK
```