Skip to content
Merged
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
31 changes: 31 additions & 0 deletions Sudoku-Solver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Sudoku-Solver

If you want a solution for a sudoku puzzle, it may help you. Give each line of the puzzle as input with zeroes in place of spaces.

This program uses backtracking process to solve. The program finds the zeroes i.e. empty spaces in the line and checks the number in range 9 that it unique in that row , column and square. Like that the code goes on checking each and every space in the puzzle. If any number is repeated inside the row and column, then it is backtrack and changes the number in the respective space. The program uses the recursion to backtrack in the puzzle.

# Input
7 8 0 4 0 0 1 2 0
6 0 0 0 7 5 0 0 9
0 0 0 6 0 1 0 7 8
0 0 7 0 4 0 2 6 0
0 0 1 0 5 0 9 3 0
9 0 4 0 6 0 0 0 5
0 7 0 3 0 0 0 1 2
1 2 0 0 0 7 4 0 0
0 4 9 2 0 6 0 0 7

# Output
7 8 5 | 4 3 9 | 1 2 6
6 1 2 | 8 7 5 | 3 4 9
4 9 3 | 6 2 1 | 5 7 8
- - - - - - - - - - - - -
8 5 7 | 9 4 3 | 2 6 1
2 6 1 | 7 5 8 | 9 3 4
9 3 4 | 1 6 2 | 7 8 5
- - - - - - - - - - - - -
5 7 8 | 3 9 4 | 6 1 2
1 2 6 | 5 8 7 | 4 9 3
3 4 9 | 2 1 6 | 8 5 7
Check for more solution?
Thank you
68 changes: 68 additions & 0 deletions Sudoku-Solver/solve_sudoku.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
def is_number_valid(row, col, num):
# function to find if the number is valid or not in the respective space
global grid # making grid as global variable
for i in range(0, 9):
# for loop to find if the number is present in the row or not
if grid[row][i] == num:
return False
for i in range(0, 9):
# for loop to find if the number is present in the column or not
if grid[i][col] == num:
return False
# makes the variables to consider the 3x3 square in the puzzle
col_subset = (col // 3) * 3
row_subset = (row // 3) * 3
for i in range(0, 3):
# for loop to find if the number is present in square
for j in range(0, 3):
if grid[row_subset + i][col_subset + j] == num:
return False
return True # if not preent in the all the possibilities returns True


def print_board(): # function to print the puzzle after solving
global grid # global variables
for i in range(len(grid)): # for loop to print the board
if i % 3 == 0 and i != 0:
print("- - - - - - - - - - - - - ")

for j in range(len(grid[0])):
if j % 3 == 0 and j != 0:
print(" | ", end="")

if j == 8:
print(grid[i][j])
else:
print(str(grid[i][j]) + " ", end="")


def solve_sudoku():
# function to solve sudoku problem
global grid
for row in range(9):
# for loop to find 0 in the row and column
for col in range(9):
if grid[row][col] == 0:
# gives values to give in the spaces
for n in range(1, 10):
# if the number is valid or not
if is_number_valid(row, col, n):
# assigns the value
grid[row][col] = n
# recalls the function
solve_sudoku()
# if the number is already present then
# assign the value 0 to it
grid[row][col] = 0
return
print("\nSoltion is:")
print_board() # calls the print_board function
# if user presses enter then function is called again
input("Check for more solution?")
# takes each line of the sudoku as input with spaces between two numbers


grid = [list(map(int, input().split()))[:9] for _ in range(9)]


solve_sudoku() # calls the solve function