MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / is_safe

Function is_safe

backtracking/sudoku.py:44–60  ·  view source on GitHub ↗

This function checks the grid to see if each row, column, and the 3x3 subgrids contain the digit 'n'. It returns False if it is not 'safe' (a duplicate digit is found) else returns True if it is 'safe'

(grid: Matrix, row: int, column: int, n: int)

Source from the content-addressed store, hash-verified

42
43
44def is_safe(grid: Matrix, row: int, column: int, n: int) -> bool:
45 """
46 This function checks the grid to see if each row,
47 column, and the 3x3 subgrids contain the digit 'n'.
48 It returns False if it is not 'safe' (a duplicate digit
49 is found) else returns True if it is 'safe'
50 """
51 for i in range(9):
52 if n in {grid[row][i], grid[i][column]}:
53 return False
54
55 for i in range(3):
56 for j in range(3):
57 if grid[(row - row % 3) + i][(column - column % 3) + j] == n:
58 return False
59
60 return True
61
62
63def find_empty_location(grid: Matrix) -> tuple[int, int] | None:

Callers 1

sudokuFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected