https://github.com/jon-becker/sudoku-solver-py
A simple project that I wrote that solves sodukos.
https://github.com/jon-becker/sudoku-solver-py
algo algorithms python soduku
Last synced: 5 months ago
JSON representation
A simple project that I wrote that solves sodukos.
- Host: GitHub
- URL: https://github.com/jon-becker/sudoku-solver-py
- Owner: Jon-Becker
- License: mit
- Created: 2021-08-28T20:20:05.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-09-09T16:39:50.000Z (over 4 years ago)
- Last Synced: 2025-07-27T09:52:04.650Z (5 months ago)
- Topics: algo, algorithms, python, soduku
- Language: Python
- Homepage:
- Size: 10.7 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sudoku-solver-py
soduku-solver-py is a simple program I challenged myself to create in 6 hours in python. Python is a language I need the most practice with, so I felt like this would be a good project to challenge that. This was a timed challenge, so there are most likely bugs, and it is not efficient at all.
## How it works
You can [View The Demo](https://jbecker.dev/demos/sudoku-solver-py) here, but basically heres the meat of the application.
- An object `Board` is generated which contains `self.board` and `self.backup`, with various helper classes such as `Board.restoreBackup()` and `Board.canPlaceNumber`.
- The `self.board` object is an array of 9 1 x 9 arrays, for example:
```
[[2,8,0,0,0,0,0,0,1],
[0,0,0,8,0,1,0,0,4],
[0,0,4,0,7,0,3,0,0],
[0,2,0,0,5,0,0,6,0],
[0,0,3,1,0,9,7,0,0],
[0,1,0,0,8,0,0,5,0],
[0,0,1,0,6,0,8,0,0],
[5,0,0,2,0,3,0,0,0],
[9,0,0,0,0,0,0,1,6]]
```
- The program then generates an array `potentialBoardValues`, and assigns each blank space an array with all numbers that could be possible numbers for that space.
- We loop through `potentialBoardValues` and find where only 1 value can be placed there. This action is repeated until there are no more guaranteed values.
- We now recursively loop through `potentialBoardValues `, assigning numbers and searching for singular possibilities, reverting to a backup board when an invalid board is encountered.
- At some point the board will be solved, which can take anywhere from 1 second to 1 minute, since its not very efficient at all.
### Todo
If I ever do revisit this project, I need to revamp the correction system so it goes back to the furthest correct point. Right now, we backup to the beginning when encountering an invalid board, which exponentially increases solve times.
### Credits
This project is completely coded by [Jonathan Becker](https://jbecker.dev), using no external libraries.