From 2d481945ba2715a40e7a54a60f42879c501c43d5 Mon Sep 17 00:00:00 2001 From: Dominik Meurer <50107525+DMeurer@users.noreply.github.com> Date: Sat, 16 Jan 2021 16:40:13 +0100 Subject: [PATCH 1/4] Meaningful commit message --- sudoku_solver/solve_sudoku.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/sudoku_solver/solve_sudoku.py b/sudoku_solver/solve_sudoku.py index 0a60c9e6b..d70666150 100644 --- a/sudoku_solver/solve_sudoku.py +++ b/sudoku_solver/solve_sudoku.py @@ -1,3 +1,34 @@ +def generate_board(num): + base = 3 + side = base * base + + def pattern(r, c): + return (base * (r % base) + r // base + c) % side + + def shuffle(s): + return sample(s, len(s)) + + # randomize rows, col, num + rBase = range(base) + rows = [g * base + r for g in shuffle(rBase) for r in shuffle(rBase)] + cols = [g * base + c for g in shuffle(rBase) for c in shuffle(rBase)] + nums = shuffle(range(1, base * base + 1)) + + # randomize baseline + board = [[nums[pattern(r, c)] for c in cols] for r in rows] + + # remove some numbers of the grid + squares = side * side + # default number of empty slots if parameter is set to 0 + if num == 0: + empties = squares * 3 // 4 + else: + empties = 81 - num # calculates number of empty solots needed. + for p in sample(range(squares), empties): + board[p // side][p % side] = 0 + + return board + 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 From 3c260c2f63a4ff735ee1b2c5981514d1b9d8c006 Mon Sep 17 00:00:00 2001 From: Dominik Meurer <50107525+DMeurer@users.noreply.github.com> Date: Sat, 16 Jan 2021 16:55:57 +0100 Subject: [PATCH 2/4] adding some comments and documentation --- sudoku_solver/README.md | 14 +++++++++----- sudoku_solver/solve_sudoku.py | 11 +++++++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/sudoku_solver/README.md b/sudoku_solver/README.md index ed18666f6..59a87f0e4 100644 --- a/sudoku_solver/README.md +++ b/sudoku_solver/README.md @@ -8,9 +8,13 @@ source .venv/bin/activate ``` 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. - + + If, like me, you're too lazy to type all of that in, you can use the generate_grid function. Put the number of preset values in the brackets, or set it to 0 if you want a defaut. + 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. - + + The generate_grid function fill the board with numbers and removes them to be sure every grid got a solution. + # Input 7 8 0 4 0 0 1 2 0 6 0 0 0 7 5 0 0 9 @@ -21,16 +25,16 @@ source .venv/bin/activate 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 diff --git a/sudoku_solver/solve_sudoku.py b/sudoku_solver/solve_sudoku.py index d70666150..acb732241 100644 --- a/sudoku_solver/solve_sudoku.py +++ b/sudoku_solver/solve_sudoku.py @@ -1,4 +1,4 @@ -def generate_board(num): +def generate_grid(num): base = 3 side = base * base @@ -17,6 +17,8 @@ def shuffle(s): # randomize baseline board = [[nums[pattern(r, c)] for c in cols] for r in rows] + # at this point there is a full/solved grid + # remove some numbers of the grid squares = side * side # default number of empty slots if parameter is set to 0 @@ -24,9 +26,10 @@ def shuffle(s): empties = squares * 3 // 4 else: empties = 81 - num # calculates number of empty solots needed. + #removes random nubers of the grid. for p in sample(range(squares), empties): board[p // side][p % side] = 0 - + #return the generated board return board def is_number_valid(row, col, num): @@ -92,8 +95,12 @@ def solve_sudoku(): input("Check for more solution?") # takes each line of the sudoku as input with spaces between two numbers +# comment out the function you want to deactivate +# input a grid by hand grid = [list(map(int, input().split()))[:9] for _ in range(9)] +# use the generate_grid function +grid = generate_grid(0) solve_sudoku() # calls the solve function From 56f5e62fe61def2b7d9c25373e169ba868f2d2a6 Mon Sep 17 00:00:00 2001 From: Dominik Meurer <50107525+DMeurer@users.noreply.github.com> Date: Sat, 16 Jan 2021 17:06:59 +0100 Subject: [PATCH 3/4] Update solve_sudoku.py --- sudoku_solver/solve_sudoku.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sudoku_solver/solve_sudoku.py b/sudoku_solver/solve_sudoku.py index acb732241..2498d7c12 100644 --- a/sudoku_solver/solve_sudoku.py +++ b/sudoku_solver/solve_sudoku.py @@ -1,4 +1,7 @@ -def generate_grid(num): +# importing sample from random to generate the grid +from random import sample + +def generate_grid(num): # function to generate a board base = 3 side = base * base @@ -93,14 +96,16 @@ def solve_sudoku(): 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 -# comment out the function you want to deactivate -# input a grid by hand -grid = [list(map(int, input().split()))[:9] for _ in range(9)] +# comment out the function you want to deactivate # use the generate_grid function grid = generate_grid(0) +# input a grid by hand +# takes each line of the sudoku as input with spaces between two numbers +grid = [list(map(int, input().split()))[:9] for _ in range(9)] + +print_board() solve_sudoku() # calls the solve function From 38604e78fc421adeab63ad501857156dbc693567 Mon Sep 17 00:00:00 2001 From: Dominik Meurer <50107525+DMeurer@users.noreply.github.com> Date: Mon, 18 Jan 2021 16:18:03 +0100 Subject: [PATCH 4/4] 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 2498d7c12..4f5bd148f 100644 --- a/sudoku_solver/solve_sudoku.py +++ b/sudoku_solver/solve_sudoku.py @@ -1,6 +1,7 @@ # importing sample from random to generate the grid from random import sample + def generate_grid(num): # function to generate a board base = 3 side = base * base @@ -28,13 +29,14 @@ def shuffle(s): if num == 0: empties = squares * 3 // 4 else: - empties = 81 - num # calculates number of empty solots needed. - #removes random nubers of the grid. + empties = 81 - num # calculates number of empty solots needed. + # removes random nubers of the grid. for p in sample(range(squares), empties): board[p // side][p % side] = 0 - #return the generated board + # return the generated board return board + 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