diff --git a/README.md b/README.md index 7cecea6..1f12a76 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Awesome Python Scripts :sunglasses: PyPI [![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) diff --git a/sudoku-solver/app.py b/sudoku-solver/app.py new file mode 100644 index 0000000..7dd3fc3 --- /dev/null +++ b/sudoku-solver/app.py @@ -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) diff --git a/sudoku-solver/readme.md b/sudoku-solver/readme.md new file mode 100644 index 0000000..0cb1063 --- /dev/null +++ b/sudoku-solver/readme.md @@ -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 +``` diff --git a/sudoku-solver/sudoku_solver/__init__.py b/sudoku-solver/sudoku_solver/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sudoku-solver/sudoku_solver/solver.py b/sudoku-solver/sudoku_solver/solver.py new file mode 100644 index 0000000..3136cb3 --- /dev/null +++ b/sudoku-solver/sudoku_solver/solver.py @@ -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