Skip to content

Instantly share code, notes, and snippets.

@hackjoy
Created January 20, 2013 13:59
Show Gist options
  • Save hackjoy/4578845 to your computer and use it in GitHub Desktop.
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.
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
@hackjoy
Copy link
Author

hackjoy commented Jan 20, 2013

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment