Skip to content

Instantly share code, notes, and snippets.

@gottagetgit
Created July 22, 2012 17:36
Show Gist options
  • Save gottagetgit/3160411 to your computer and use it in GitHub Desktop.
Save gottagetgit/3160411 to your computer and use it in GitHub Desktop.
Udacity CS258 - Sudoku Checker
def get_groups():
q = lambda f:[[f(i,j) for j in range(9)] for i in range(9)]
return q(lambda i,j:(i,j)) + q(lambda i,j:(j,i)) + q(lambda i,j:(i/3*3+j/3,i%3*3+j%3))
# I think this is more readable than the one-liner:
# >> return [l for i in range(9) for l in zip(*[[(i,j),(j,i),(i/3*3+j/3,i%3*3+j%3)] for j in range(9)])]
def check_sudoku(grid):
# Check to see if grid is a list with 9 elements
if ((not type(grid) is list) or (len(grid) != 9)): return None
# Check to see if each row is a list with 9 elements
for row in grid:
if ((not type(row) is list) or (len(row) != 9)): return None
if set(map(len,grid)) != set([9]): return None
if min(map(min,grid)) < 0: return None
if max(map(max,grid)) > 9: return None
for s in get_groups():
l = filter(None,[grid[i][j] for i,j in s])
if len(l) != len(set(l)): return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment