def block_naked_single(self, grid, x, y): """ Remove from the (x, y) cell all the candidates that are already present on the same 3x3 block. Returns True if the cell is solved (ie has only one remaining candidate) after this process, False otherwise. """ if grid.get_solution(x, y): return False xBlock, yBlock = startCoordinatesOfBlock(x, y) removed_candidates = [] for i in range(3): for j in range(3): if xBlock + i == x and yBlock + j == y: continue peer = grid.get_solution(xBlock + i, yBlock + j) if peer and peer in grid.candidates[x][y]: removed_candidates.append(peer) grid.remove_candidate(x, y, peer) log_removals("block", removed_candidates, x, y) return grid.get_solution(x, y) != None
def block_hidden_single(self, grid, x, y): """ For each of the remaining candidates in the (x, y) cell, determine if there is no other cell in the same block having the same digit among its remaining candidates. If not, since each digit must appear once in each block, it means that cell must hold that digit. Returns True if such a hidden single was found in this cell, False otherwise. """ if grid.get_solution(x, y): return False (xBlock, yBlock) = startCoordinatesOfBlock(x, y) for digit in grid.candidates[x][y]: notFound = True for i in range(3): for j in range(3): if xBlock + i == x and yBlock + j == y: continue if digit in grid.candidates[xBlock + i][yBlock + j]: notFound = False break if notFound: grid.set(x, y, digit) return True return False
def test_start_coordinates_of_block(self): self.assertEqual((3, 0), startCoordinatesOfBlock(4, 1)) self.assertEqual((0, 0), startCoordinatesOfBlock(0, 0)) self.assertEqual((6, 6), startCoordinatesOfBlock(7, 6)) self.assertEqual((0, 6), startCoordinatesOfBlock(2, 8))