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)
| 42 | |
| 43 | |
| 44 | def 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 | |
| 63 | def find_empty_location(grid: Matrix) -> tuple[int, int] | None: |