Question: A sudoku puzzle in python. Suppose to check if the state is solved or not. I have two methods the goalChecker. I am not sure

A sudoku puzzle in python. Suppose to check if the state is solved or not. I have two methods the goalChecker. I am not sure if I am going towards the right step with the code I have so far. I want to make a set that I can use to check for any duplication however I do not exactly know how I would do that with the input being a 2D matrix.

here is the input and the parameter for reference

showDomain=True board = [ [7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 0, 0, 0, 7, 5, 0, 0, 9], [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 0, 7, 0, 4, 0, 2, 6, 0], [0, 0, 1, 0, 5, 0, 9, 3, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5], [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] ] # board = [ # [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, 0, 0] # ]

def __init__(self, rows, cols, width, height, win): self.rows = rows self.cols = cols self.cells = [[Cell(self.board[i][j], i, j, width, height) for j in range(cols)] for i in range(rows)] self.width = width self.height = height self.model = None self.update_model() self.selected = None self.win = win

Here is the function I want to use:

def goalCheck(self,state): ''' This function takes a parameter: state of type list[list[int]] which holds current board value which you can use to check if the board is solved or not.

Function returns a boolean value ''' cellSize = 9 #check for any empty space #checks the cols for row in range(0,cellSize): if self.cells[row][self.cols].value == 0: return False #checks the row for col in range(0,cellSize): if self.cells[self.rows][col] == 0: return False #check for numbers that is already there #check the value of the row for any dublicates #in the cube x = (self.cols //3)*3 y = (self.rows //3)*3 for i in range(0,3): if self.cells[x][y] == 0: return False #check for the dublicates check_r = set(self.rows) check_c = set(self.cols)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!