Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def solve_sudoku(file_name):
- ''' Solves sudoku given in a .txt file'''
- list_of_empties = None
- #TODO: Open up file and get a Python readable grid
- sudoku_grid = get_grid_from_file(file_name)
- #TODO: Get length of sudoku
- column_length = len(sudoku_grid)
- row_length = len(sudoku_grid[1])
- while len(list_of_empties) > 0 or list_of_empties == None:
- #Returns a list of all the empty blanks and the possible valid entries in each blank
- #TODO: Check all the blanks for valid entries
- for r in range(row_length):
- for c in range(column_length):
- #Finds and checks the value in the r-th row and c-th column if it is empty.
- if subgrid_values(sudoku_grid,r,c) == 'x':
- list_of_valid_entries = []
- #Checks all the possible valid entries for that particular blank of the sudoku
- for i in range (1,10):
- if valid_entry(sudoku_grid,i,r,c):
- list_of_valid_entries.append(i)
- #Stores the infomation (the number of possible valid entries to the blank, the list of valid entries, the row and column of the empty) in a list of all the empty blanks.
- list_of_empties.append([len(list_of_valid_entries),list_of_valid_entries,r,c])
- #TODO: Solve starting with the blanks with the least number of valid entries.
- least_valid_entry = None
- for empty in list_of_empties:
- #Finds the empty blanks with the least number of valid entries
- if least_valid_entry == None:
- least_valid_entry = empty
- elif empty[0] < least_valid_entry[0]:
- least_valid_entry = empty
- #If there is only one valid entry for that empty blank, replace the empty blank with the values
- if least_valid_entry[0] == 1:
- sudoku_grid[least_valid_entry[3]][least_valid_entry[2]] = least_valid_entry[1][0]
- #Repeats the process till there is no more empty blanks left
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement