forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckSudokuSolution.py
29 lines (25 loc) · 1.04 KB
/
CheckSudokuSolution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Check whether a solution is valid
def isValid(grid):
for i in range(9):
for j in range(9):
if grid[i][j] < 1 or grid[i][j] > 9 \
or not isValidAt(i, j, grid):
return False
return True # The fixed cells are valid
# Check whether grid[i][j] is valid in the grid
def isValidAt(i, j, grid):
# Check whether grid[i][j] is valid at the i's row
for column in range(9):
if column != j and grid[i][column] == grid[i][j]:
return False
# Check whether grid[i][j] is valid at the j's column
for row in range(9):
if row != i and grid[row][j] == grid[i][j]:
return False
# Check whether grid[i][j] is valid in the 3 by 3 box
for row in range((i // 3) * 3, (i // 3) * 3 + 3):
for col in range((j // 3) * 3, (j // 3) * 3 + 3):
if row != i and col != j and \
grid[row][col] == grid[i][j]:
return False
return True # The current value at grid[i][j] is valid