Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Sudoku Solver python program #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Python/SudokuSolver/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""

"""
27 changes: 27 additions & 0 deletions Python/SudokuSolver/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Runner file for Sudoku Solver

"""

from SudokuSolver.solver import print_sudoku, solve

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--------")
print_sudoku(x)
input("Solve ?")
solve(x)
print("-------OUTPUT--------")
print_sudoku(x)
38 changes: 38 additions & 0 deletions Python/SudokuSolver/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#Sudoku Solver Using Backtracking

Run main.py to view the game

```buildoutcfg
---------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

Solve ?y

-------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


```

Update grid in main.py to try your own sudoku
- Ashhar farooqui --> Happy Coding guys !
48 changes: 48 additions & 0 deletions Python/SudokuSolver/solver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Solver functions for Sudoku

"""

def print_sudoku(sudoku):
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)

def get_next_empty_cell(sudoku):
for x in range(9):
for y in range(9):
if sudoku[x][y] == 0:
return x, y
return -1, -1

def is_possible(sudoku, i, j, e):
check_row = all([e != sudoku[i][x] for x in range(9)])
if check_row:
check_col = all([e != sudoku[x][j] for x in range(9)])
if check_col:
sq_x, sq_y = 3*(i//3), 3*(j//3)
for x in range(sq_x, sq_x+3):
for y in range(sq_y, sq_y+3):
if sudoku[x][y] == e:
return False
return True
return False

def solve(sudoku, i=0, j=0):
i, j = get_next_empty_cell(sudoku)
if i == -1:
return True
for n in range(1, 10):
if is_possible(sudoku, i, j, n):
sudoku[i][j] = n
if solve(sudoku, i, j):
return True
sudoku[i][j] = 0
return False