Created
January 20, 2013 13:59
-
-
Save hackjoy/4578845 to your computer and use it in GitHub Desktop.
Validates that sudoku grid contains once instance of each number per column and row.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_sudoku(grid): | |
n = len(grid) | |
digit = 1 | |
while digit <= n: | |
i = 0 | |
while i < n: | |
row_count = 0 | |
column_count = 0 | |
j = 0 | |
while j < n: | |
if grid[i][j] == digit: | |
row_count = row_count + 1 | |
if grid[j][i] == digit: | |
column_count = column_count + 1 | |
j = j + 1 | |
if column_count != 1 or row_count != 1: | |
return False | |
i = i + 1 | |
digit = digit + 1 | |
return True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In a 3x3 grid:
i = 0 , j = 0 and is incremented until same as length of grid
[0][0], [0][1], [0][2]
[0][0], [1][0], [2][0]
i is incremented by 1
i = 1 , j = 0 and is incremented until same as length of grid
[1][0], [1][1], [1][2]
[0][1], [1][1], [2][1]
i is incremented by 1
i = 2 , j = 0 and is incremented until same as length of grid
[2][0], [2][1], [2][2]
[0][2], [1][2], [2][2]