From 4d370c4545d67566dd01b573884b1d3ec9aa6bac Mon Sep 17 00:00:00 2001 From: jayaganeshkumar <56192588+jayaganeshkumar@users.noreply.github.com> Date: Thu, 8 Oct 2020 19:44:30 +0530 Subject: [PATCH 1/6] Create Sudoku-Solver --- Sudoku-Solver | 1 + 1 file changed, 1 insertion(+) create mode 100644 Sudoku-Solver diff --git a/Sudoku-Solver b/Sudoku-Solver new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/Sudoku-Solver @@ -0,0 +1 @@ + From c072fe5e1c4570183ba08a3def56d3e1fa9b248e Mon Sep 17 00:00:00 2001 From: jayaganeshkumar <56192588+jayaganeshkumar@users.noreply.github.com> Date: Thu, 8 Oct 2020 19:45:27 +0530 Subject: [PATCH 2/6] Delete Sudoku-Solver --- Sudoku-Solver | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Sudoku-Solver diff --git a/Sudoku-Solver b/Sudoku-Solver deleted file mode 100644 index 8b1378917..000000000 --- a/Sudoku-Solver +++ /dev/null @@ -1 +0,0 @@ - From 6f5c9280ae6eebd7abb8bb9217dddcf3b959128f Mon Sep 17 00:00:00 2001 From: jayaganeshkumar <56192588+jayaganeshkumar@users.noreply.github.com> Date: Thu, 8 Oct 2020 19:46:21 +0530 Subject: [PATCH 3/6] Added Sudoku Solver python script --- Sudoku-Solver/README.md | 31 ++++++++++++++++++++++ Sudoku-Solver/solve_sudoku.py | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 Sudoku-Solver/README.md create mode 100644 Sudoku-Solver/solve_sudoku.py diff --git a/Sudoku-Solver/README.md b/Sudoku-Solver/README.md new file mode 100644 index 000000000..b9eb5883e --- /dev/null +++ b/Sudoku-Solver/README.md @@ -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 diff --git a/Sudoku-Solver/solve_sudoku.py b/Sudoku-Solver/solve_sudoku.py new file mode 100644 index 000000000..bf1ea94f2 --- /dev/null +++ b/Sudoku-Solver/solve_sudoku.py @@ -0,0 +1,49 @@ +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 + col_subset = (col//3) * 3 #makes the variables to consider the 3x3 square in the puzzle + 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: + for n in range (1,10): #gives values to give in the spaces + if is_number_valid(row,col,n): #if the number is valid or not + grid[row][col] = n #assigns the value + solve_sudoku() #recalls the function + grid[row][col] = 0 #if the number is already present then assign the value 0 to it + return + print("\nSoltion is:") + print_board() #calls the print_board function + input("Check for more solution?") #if user presses enter then function is called again + +#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 \ No newline at end of file From 10822628a4183ca296e84e1f463fc0ecb8f3c35c Mon Sep 17 00:00:00 2001 From: jayaganeshkumar <56192588+jayaganeshkumar@users.noreply.github.com> Date: Thu, 8 Oct 2020 20:14:31 +0530 Subject: [PATCH 4/6] Update solve_sudoku.py --- Sudoku-Solver/solve_sudoku.py | 65 +++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/Sudoku-Solver/solve_sudoku.py b/Sudoku-Solver/solve_sudoku.py index bf1ea94f2..02e88627b 100644 --- a/Sudoku-Solver/solve_sudoku.py +++ b/Sudoku-Solver/solve_sudoku.py @@ -1,22 +1,28 @@ -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 +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 + 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 - col_subset = (col//3) * 3 #makes the variables to consider the 3x3 square in the puzzle + # 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): + 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 + 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 + +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("- - - - - - - - - - - - - ") @@ -28,22 +34,31 @@ def print_board(): #function to print the puzzle after solving print(grid[i][j]) else: print(str(grid[i][j]) + " ", end="") - -def solve_sudoku(): #function to solve sudoku problem + + +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 row in range(9): + # for loop to find 0 in the row and column for col in range(9): if grid[row][col] == 0: - for n in range (1,10): #gives values to give in the spaces - if is_number_valid(row,col,n): #if the number is valid or not - grid[row][col] = n #assigns the value - solve_sudoku() #recalls the function - grid[row][col] = 0 #if the number is already present then assign the value 0 to it + # 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 - input("Check for more solution?") #if user presses enter then function is called again - -#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 \ No newline at end of file + 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 From b290633bbfdad0901e29a351a6d3fe3ac1047200 Mon Sep 17 00:00:00 2001 From: jayaganeshkumar <56192588+jayaganeshkumar@users.noreply.github.com> Date: Thu, 8 Oct 2020 20:17:43 +0530 Subject: [PATCH 5/6] Update solve_sudoku.py --- Sudoku-Solver/solve_sudoku.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sudoku-Solver/solve_sudoku.py b/Sudoku-Solver/solve_sudoku.py index 02e88627b..6931d5c22 100644 --- a/Sudoku-Solver/solve_sudoku.py +++ b/Sudoku-Solver/solve_sudoku.py @@ -10,12 +10,12 @@ def is_number_valid(row, col, num): 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 + 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: + if grid[row_subset + i][col_subset + j] == num: return False return True # if not preent in the all the possibilities returns True @@ -61,4 +61,6 @@ def solve_sudoku(): 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 From f908d2bc6009e41fe1c49d6ca0c8b40f6bb5a27f Mon Sep 17 00:00:00 2001 From: jayaganeshkumar <56192588+jayaganeshkumar@users.noreply.github.com> Date: Thu, 8 Oct 2020 20:19:32 +0530 Subject: [PATCH 6/6] Update solve_sudoku.py --- Sudoku-Solver/solve_sudoku.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sudoku-Solver/solve_sudoku.py b/Sudoku-Solver/solve_sudoku.py index 6931d5c22..0a60c9e6b 100644 --- a/Sudoku-Solver/solve_sudoku.py +++ b/Sudoku-Solver/solve_sudoku.py @@ -60,6 +60,8 @@ def solve_sudoku(): # 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)]