If not is_valid_grid(fill_singles(test_grid)):įiltered_candidates.remove(candidate)ĭef merge(candidates_1 : list, candidates_2 : list) -> list: # Remove candidate if it produces an invalid grid """Filter input grid's list of candidates"""įiltered_candidates = deepcopy(candidates) Np.all(np.sum(get_subgrids(grid), axis=1) = 45):ĭef filter_candidates(grid : np.ndarray) -> list: If np.all(np.sum(grid, axis=1) = 45) and \ """Verify if the input grid is a solution""" """Verify the input grid has a possible solution"""ĭef is_solution(grid : np.ndarray) -> bool: # If the case is filled take its value as the only candidateĭef is_valid_grid(grid : np.ndarray) -> bool: """Get a list of candidates to fill empty cells of the input grid""" Subgrid.append(grid)ĭef get_candidates(grid : np.ndarray) -> list: """Divide the input grid into 9 3x3 sub-grids""" Lines = puzzle_str.replace(' ','').replace('\n','')ĭef get_subgrids(grid: np.ndarray) -> np.ndarray: """Create a 9x9 Sudoku grid from a string of digits""" from copy import deepcopyĭef create_grid(puzzle_str: str) -> np.ndarray: It then recursively calls solve to either find a solution or reach a no-solution grid (in which case solve returns None and the last guess changes are reverted). This function will fill the next empty cell with the least quantity of candidates with one of its candidates, a "guess" value. There's a clause to return None which is used to backtrack changes by the make_guess function. If this process leads to a completely filled Sudoku grid, it's returned as a solution. "Wrong candidates" are those that when filled to a empty cell, led to another cell having no more candidates elsewhere on the Sudoku grid.Īfter filtering candidates, fill_singles is called to fill empty cells that have only one remaining candidate. First, it discards wrong candidates with the filter-candidates function. The get_candidates function creates lists of possible digits to fill each cell following Sudoku's rules (no repeating 1-9 digit along rows, columns and 3x3 sub-grids). The Solver works by accepting a string of 81 digits for the Sudoku puzzle input. I'd appreciate your comments about coding style, structure and how to improve it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |