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 0a60c9e6b..4f5bd148f 100644 --- a/sudoku_solver/solve_sudoku.py +++ b/sudoku_solver/solve_sudoku.py @@ -1,3 +1,42 @@ +# 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 + + 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] + + # 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 + if num == 0: + 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): # function to find if the number is valid or not in the respective space global grid # making grid as global variable @@ -59,10 +98,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 -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