From b74653eee3a88d670456f6c98be85061af822d9c Mon Sep 17 00:00:00 2001 From: SandeepaDilshanAlagiyawanna Date: Mon, 16 Oct 2023 17:51:17 +0530 Subject: [PATCH 1/6] Added more optimized sudoku solver algorithm --- backtracking/sucoku-solver-optimized.py | 109 ++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 backtracking/sucoku-solver-optimized.py diff --git a/backtracking/sucoku-solver-optimized.py b/backtracking/sucoku-solver-optimized.py new file mode 100644 index 000000000000..78a5db75cb4b --- /dev/null +++ b/backtracking/sucoku-solver-optimized.py @@ -0,0 +1,109 @@ +def is_valid(board, row, col, num): + """ Checks if the given number is valid for the given cell in the Sudoku board """ + for i in range(9): + if ( + # checking is there any same number in row + board[row][i] == num or + # checking is there any same number in column + board[i][col] == num or + # checking is there any same number in cell(3*3) + board[(row // 3) * 3 + i // 3][(col // 3) * 3 + i % 3] == num + ): + # if so return false + return False + return True + + +"""Taking the Board and checking the availability""" + + +def solve_sudoku(board): + # use of recursive function solve. + def solve(row, col): + + # if row number is 9, return true as there is no any other row (last Row Solved) + if row == 9: + return True + + # if column number is 9, then we should change the row and check again + if col == 9: + # resetting row number to next and column number to 0 + return solve(row + 1, 0) + + # if the number of the board[row][col] is not the 0, then it is solved, then we should move o next. + if board[row][col] != 0: + # moving to next column + return solve(row, col + 1) + + # setting a number between 1-9 + for num in range(1, 10): + + # checking the availability of that number + if is_valid(board, row, col, num): + + # if the number is ok,then setting the number + board[row][col] = num + + # moving to next column + if solve(row, col + 1): + return True + + # if none above are not fitting, lets hold the value of cell as zero, and then we can call it again + board[row][col] = 0 + + # returning none as algorithm cannot find any solution + return False + + # starting with (0,0) cell + if solve(0, 0): + + # return the solution + return board + else: + + # no solution + return None + + +""" printing the board """ + + +def print_board(board): + if board: + + # printing the board row by row + for row in board: + + # joining the int as strings by including space in between + print(" ".join(map(str, row))) + else: + + print("No solution found.") + + +def main(): + + board = [ + [0, 0, 3, 0, 0, 0, 0, 0, 0], + [0, 4, 0, 2, 0, 0, 6, 3, 0], + [0, 8, 2, 1, 0, 0, 0, 0, 9], + [0, 0, 0, 4, 0, 6, 0, 0, 0], + [5, 0, 0, 0, 9, 0, 1, 0, 0], + [3, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 7, 8, 0], + [0, 0, 0, 9, 0, 0, 0, 6, 0], + [0, 0, 1, 7, 2, 0, 4, 0, 0] + ] + + # taking the solution board + solution = solve_sudoku(board) + + if solution: + print("The solved Sudoku puzzle:") + print_board(solution) + else: + print("No solution found.") + + +if __name__ == "__main__": + main() From 5a66a76d9eb85b275a6df79eaa76f02a32908b58 Mon Sep 17 00:00:00 2001 From: SandeepaDilshanAlagiyawanna Date: Mon, 16 Oct 2023 17:56:25 +0530 Subject: [PATCH 2/6] Added more optimized sudoku solver algorithm and File Renamed --- .../{sucoku-solver-optimized.py => sucoku_solver_optimized.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename backtracking/{sucoku-solver-optimized.py => sucoku_solver_optimized.py} (100%) diff --git a/backtracking/sucoku-solver-optimized.py b/backtracking/sucoku_solver_optimized.py similarity index 100% rename from backtracking/sucoku-solver-optimized.py rename to backtracking/sucoku_solver_optimized.py From 76c5a589ef798a711e753429ae9a52987ed7dc87 Mon Sep 17 00:00:00 2001 From: SandeepaDilshanAlagiyawanna Date: Mon, 16 Oct 2023 18:09:12 +0530 Subject: [PATCH 3/6] and_gate is Optimized --- boolean_algebra/and_gate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boolean_algebra/and_gate.py b/boolean_algebra/and_gate.py index f0fd45c9f81e..6ae66b5b0a77 100644 --- a/boolean_algebra/and_gate.py +++ b/boolean_algebra/and_gate.py @@ -29,7 +29,7 @@ def and_gate(input_1: int, input_2: int) -> int: >>> and_gate(1, 1) 1 """ - return int((input_1, input_2).count(0) == 0) + return int(input_1 and input_2) if __name__ == "__main__": From 48e0d90f438619b7aa9064e7a8d4a90b62a61256 Mon Sep 17 00:00:00 2001 From: SandeepaDilshanAlagiyawanna Date: Mon, 16 Oct 2023 18:09:50 +0530 Subject: [PATCH 4/6] and_gate is Optimized --- backtracking/sucoku_solver_optimized.py | 109 ------------------------ 1 file changed, 109 deletions(-) delete mode 100644 backtracking/sucoku_solver_optimized.py diff --git a/backtracking/sucoku_solver_optimized.py b/backtracking/sucoku_solver_optimized.py deleted file mode 100644 index 78a5db75cb4b..000000000000 --- a/backtracking/sucoku_solver_optimized.py +++ /dev/null @@ -1,109 +0,0 @@ -def is_valid(board, row, col, num): - """ Checks if the given number is valid for the given cell in the Sudoku board """ - for i in range(9): - if ( - # checking is there any same number in row - board[row][i] == num or - # checking is there any same number in column - board[i][col] == num or - # checking is there any same number in cell(3*3) - board[(row // 3) * 3 + i // 3][(col // 3) * 3 + i % 3] == num - ): - # if so return false - return False - return True - - -"""Taking the Board and checking the availability""" - - -def solve_sudoku(board): - # use of recursive function solve. - def solve(row, col): - - # if row number is 9, return true as there is no any other row (last Row Solved) - if row == 9: - return True - - # if column number is 9, then we should change the row and check again - if col == 9: - # resetting row number to next and column number to 0 - return solve(row + 1, 0) - - # if the number of the board[row][col] is not the 0, then it is solved, then we should move o next. - if board[row][col] != 0: - # moving to next column - return solve(row, col + 1) - - # setting a number between 1-9 - for num in range(1, 10): - - # checking the availability of that number - if is_valid(board, row, col, num): - - # if the number is ok,then setting the number - board[row][col] = num - - # moving to next column - if solve(row, col + 1): - return True - - # if none above are not fitting, lets hold the value of cell as zero, and then we can call it again - board[row][col] = 0 - - # returning none as algorithm cannot find any solution - return False - - # starting with (0,0) cell - if solve(0, 0): - - # return the solution - return board - else: - - # no solution - return None - - -""" printing the board """ - - -def print_board(board): - if board: - - # printing the board row by row - for row in board: - - # joining the int as strings by including space in between - print(" ".join(map(str, row))) - else: - - print("No solution found.") - - -def main(): - - board = [ - [0, 0, 3, 0, 0, 0, 0, 0, 0], - [0, 4, 0, 2, 0, 0, 6, 3, 0], - [0, 8, 2, 1, 0, 0, 0, 0, 9], - [0, 0, 0, 4, 0, 6, 0, 0, 0], - [5, 0, 0, 0, 9, 0, 1, 0, 0], - [3, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 7, 8, 0], - [0, 0, 0, 9, 0, 0, 0, 6, 0], - [0, 0, 1, 7, 2, 0, 4, 0, 0] - ] - - # taking the solution board - solution = solve_sudoku(board) - - if solution: - print("The solved Sudoku puzzle:") - print_board(solution) - else: - print("No solution found.") - - -if __name__ == "__main__": - main() From 8ea8792b4932a1e7a68c09ce2326189f65f51062 Mon Sep 17 00:00:00 2001 From: SandeepaDilshanAlagiyawanna Date: Mon, 16 Oct 2023 18:15:59 +0530 Subject: [PATCH 5/6] and_gate is Optimized --- boolean_algebra/nand_gate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boolean_algebra/nand_gate.py b/boolean_algebra/nand_gate.py index 80f9d12db89a..fd2b633fb258 100644 --- a/boolean_algebra/nand_gate.py +++ b/boolean_algebra/nand_gate.py @@ -27,7 +27,7 @@ def nand_gate(input_1: int, input_2: int) -> int: >>> nand_gate(1, 1) 0 """ - return int((input_1, input_2).count(0) != 0) + return int(not(input_1 and input_2)) if __name__ == "__main__": From 7538d7465a3ac05d267d23c88d88b9378b0557fb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 16 Oct 2023 12:46:50 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boolean_algebra/nand_gate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boolean_algebra/nand_gate.py b/boolean_algebra/nand_gate.py index fd2b633fb258..ea7a6815dcc9 100644 --- a/boolean_algebra/nand_gate.py +++ b/boolean_algebra/nand_gate.py @@ -27,7 +27,7 @@ def nand_gate(input_1: int, input_2: int) -> int: >>> nand_gate(1, 1) 0 """ - return int(not(input_1 and input_2)) + return int(not (input_1 and input_2)) if __name__ == "__main__":