sudoku-solver (#144)

This commit is contained in:
ayedaemon 2020-09-26 12:13:51 +05:30 committed by GitHub
parent 6e869244dd
commit 7d0ac020b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 133 additions and 2 deletions

View File

@ -1,5 +1,5 @@
# Awesome Python Scripts :sunglasses: <img alt="PyPI" src="https://warehouse-camo.cmh1.psfhosted.org/18509a25dde64f893bd96f21682bd6211c3d4e80/68747470733a2f2f696d672e736869656c64732e696f2f707970692f707976657273696f6e732f64796e61636f6e662e737667"> [![HitCount](http://hits.dwyl.io/hastagAB/Awesome-Python-Scripts.svg)](http://hits.dwyl.io/hastagAB/Awesome-Python-Scripts) ![GitHub stars](https://img.shields.io/github/stars/hastagAB/Awesome-Python-Scripts?style=social)
## What is this repo?
This repo is a compilation of some *awesome* Python scripts that automate some boring tasks or simply make our life easier...or both!
@ -11,6 +11,7 @@ So far, the following projects have been integrated to this repo:
| Project Name | Contributors |
|--|--|
| [sudoku-solver](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/sudoku-solver) | [Rishabh Umrao](https://github.com/ayedaemon) |
|[File Encrypt Decrypt](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/file-encrypt-decrypt)|[Aditya Arakeri](https://github.com/adityaarakeri)|
| [Address locator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Location_Of_Adress) | [Chris]() |
| [Automated emails](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/automated_email) | [Suvigya](https://github.com/SuvigyaJain1) |
@ -191,7 +192,7 @@ Remember to star the repo if you love the scripts~ :wink:
# Want to connect with me ?
- [LinkedIn](https://www.linkedin.com/in/hastagab/)
- [Twitter](https://twitter.com/HastagAB)
- [Facebook](https://www.facebook.com/SirHastagAB)
- [Facebook](https://www.facebook.com/SirHastagAB)
- [Quora](https://www.quora.com/profile/Ayush-Bhardwaj-76)
[![](https://img.shields.io/badge/Made%20With%20❤%20By-HastagAB-red)](https://github.com/hastagAB)

21
sudoku-solver/app.py Normal file
View File

@ -0,0 +1,21 @@
from sudoku_solver.solver import *
x = [[8, 1, 0, 0, 3, 0, 0, 2, 7],
[0, 6, 2, 0, 5, 0, 0, 9, 0],
[0, 7, 0, 0, 0, 0, 0, 0, 0],
[0, 9, 0, 6, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 2, 0, 0, 0, 4],
[0, 0, 8, 0, 0, 5, 0, 7, 0],
[0, 0, 0, 0, 0, 0, 0, 8, 0],
[0, 2, 0, 0, 1, 0, 7, 5, 0],
[3, 8, 0, 0, 7, 0, 0, 4, 2]]
if __name__ == "__main__":
print("INPUT => ")
printsudoku(x)
solveSudoku(x)
print("OUTPUT => ")
printsudoku(x)

65
sudoku-solver/readme.md Normal file
View File

@ -0,0 +1,65 @@
# sudoku-solver
This is a script to solve 9x9 sudoku matrix using python.
### How to use it?
1. edit app.py to add your sudoku matrix. (Fill `0` for empty cells.)
```
For example,
[[8, 1, 0, 0, 3, 0, 0, 2, 7],
[0, 6, 2, 0, 5, 0, 0, 9, 0],
[0, 7, 0, 0, 0, 0, 0, 0, 0],
[0, 9, 0, 6, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 2, 0, 0, 0, 4],
[0, 0, 8, 0, 0, 5, 0, 7, 0],
[0, 0, 0, 0, 0, 0, 0, 8, 0],
[0, 2, 0, 0, 1, 0, 7, 5, 0],
[3, 8, 0, 0, 7, 0, 0, 4, 2]]
```
2. run the script.
```
python3 app.py
```
3. This will give you output on the console. Output will contain the input sudoku matrix and the solved sudoku matrix.
```
INPUT =>
8 1 0 | 0 3 0 | 0 2 7
0 6 2 | 0 5 0 | 0 9 0
0 7 0 | 0 0 0 | 0 0 0
---------------------
0 9 0 | 6 0 0 | 1 0 0
1 0 0 | 0 2 0 | 0 0 4
0 0 8 | 0 0 5 | 0 7 0
---------------------
0 0 0 | 0 0 0 | 0 8 0
0 2 0 | 0 1 0 | 7 5 0
3 8 0 | 0 7 0 | 0 4 2
OUTPUT =>
8 1 9 | 4 3 6 | 5 2 7
4 6 2 | 7 5 1 | 3 9 8
5 7 3 | 2 9 8 | 4 1 6
---------------------
2 9 4 | 6 8 7 | 1 3 5
1 5 7 | 9 2 3 | 8 6 4
6 3 8 | 1 4 5 | 2 7 9
---------------------
7 4 5 | 3 6 2 | 9 8 1
9 2 6 | 8 1 4 | 7 5 3
3 8 1 | 5 7 9 | 6 4 2
```

View File

View File

@ -0,0 +1,44 @@
def printsudoku(sudoku):
print("\n\n")
for i in range(len(sudoku)):
line = ""
if i == 3 or i == 6:
print("---------------------")
for j in range(len(sudoku[i])):
if j == 3 or j == 6:
line += "| "
line += str(sudoku[i][j])+" "
print(line)
print("\n\n")
def findNextCellToFill(sudoku):
for x in range(9):
for y in range(9):
if sudoku[x][y] == 0:
return x, y
return -1, -1
def isValid(sudoku, i, j, e):
rowOk = all([e != sudoku[i][x] for x in range(9)])
if rowOk:
columnOk = all([e != sudoku[x][j] for x in range(9)])
if columnOk:
secTopX, secTopY = 3*(i//3), 3*(j//3)
for x in range(secTopX, secTopX+3):
for y in range(secTopY, secTopY+3):
if sudoku[x][y] == e:
return False
return True
return False
def solveSudoku(sudoku, i=0, j=0):
i, j = findNextCellToFill(sudoku)
if i == -1:
return True
for e in range(1, 10):
if isValid(sudoku, i, j, e):
sudoku[i][j] = e
if solveSudoku(sudoku, i, j):
return True
sudoku[i][j] = 0
return False