Home > other >  Trying to Understand a python code to build up a Sudoku Solver
Trying to Understand a python code to build up a Sudoku Solver

Time:10-28

def init_valid(self):
    # Fill out dictionary V such that V[(r,c)], where (r,c) are the coordinates of a cell, contains the set of integers that can be 
    # written in cell (r,c) without breaking any of the rules
    # If a number has already been written in cell (r,c), then V[(r,c)] should contain the empty set 
    self.V = {(i,j):{0,1,2,3,4,5,6,7,8,9} - set(self.S[value[0]][value[1]] for value in self.N[(i,j)]) for i in range(9) for j in range(9)} 

Hello everyone, I am currently working on a project to build up a Sudoku Solver on python and I am just trying to understand the syntax and what is doing this line of self.V, it fills out the numbers that can be written in a specific coordinate corresponding a position giving the sudoku rules that if the number is written is the row, column or 3 by 3 section is taken out from the V dictionary, any insights would be really appreciated

CodePudding user response:

Your code is a super compact version of the following:

self.V = {}
for i in range(9):
    for j in range(9):
        
        my_set = set()
        for value in self.N(i, j):
            my_set.add(self.S[value[0]][value[1]])    
        
        self.V[(i, j)] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - my_set
  • Related