Skip to content

Instantly share code, notes, and snippets.

@sungamagnus
Created July 17, 2012 00:09
Show Gist options
  • Select an option

  • Save sungamagnus/3126026 to your computer and use it in GitHub Desktop.

Select an option

Save sungamagnus/3126026 to your computer and use it in GitHub Desktop.
my check_sudoku for udacity cs258 Unit 3 - HW 1
#!/usr/bin/env python
# SPECIFICATION:
#
# check_sudoku() determines whether its argument is a valid Sudoku
# grid. It can handle grids that are completely filled in, and also
# grids that hold some empty cells where the player has not yet
# written numbers.
#
# First, your code must do some sanity checking to make sure that its
# argument:
#
# - is a 9x9 list of lists
#
# - contains, in each of its 81 elements, a number in the range 0..9
#
# If either of these properties does not hold, check_sudoku must
# return None.
#
# If the sanity checks pass, your code should return True if all of
# the following hold, and False otherwise:
#
# - each number in the range 1..9 occurs only once in each row
#
# - each number in the range 1..9 occurs only once in each column
#
# - each number the range 1..9 occurs only once in each of the nine
# 3x3 sub-grids, or "boxes", that make up the board
#
# This diagram (which depicts a valid Sudoku grid) illustrates how the
# grid is divided into sub-grids:
#
# 5 3 4 | 6 7 8 | 9 1 2
# 6 7 2 | 1 9 5 | 3 4 8
# 1 9 8 | 3 4 2 | 5 6 7
# ---------------------
# 8 5 9 | 7 6 1 | 4 2 3
# 4 2 6 | 8 5 3 | 7 9 1
# 7 1 3 | 9 2 4 | 8 5 6
# ---------------------
# 9 6 1 | 5 3 7 | 0 0 0
# 2 8 7 | 4 1 9 | 0 0 0
# 3 4 5 | 2 8 6 | 0 0 0
#
# Please keep in mind that a valid grid (i.e., one for which your
# function returns True) may contain 0 multiple times in a row,
# column, or sub-grid. Here we are using 0 to represent an element of
# the Sudoku grid that the player has not yet filled in.
# check_sudoku should return None
ill_formed = [[5,3,4,6,7,8,9,1,2],
[6,7,2,1,9,5,3,4,8],
[1,9,8,3,4,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9], # <---
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,2,8,6,1,7,9]]
# check_sudoku should return True
valid = [[5,3,4,6,7,8,9,1,2],
[6,7,2,1,9,5,3,4,8],
[1,9,8,3,4,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9,1],
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,2,8,6,1,7,9]]
# check_sudoku should return False
invalid = [[5,3,4,6,7,8,9,1,2],
[6,7,2,1,9,5,3,4,8],
[1,9,8,3,8,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9,1],
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,2,8,6,1,7,9]]
# check_sudoku should return True
easy = [[2,9,0,0,0,0,0,7,0],
[3,0,6,0,0,8,4,0,0],
[8,0,0,0,4,0,0,0,2],
[0,2,0,0,3,1,0,0,7],
[0,0,0,0,8,0,0,0,0],
[1,0,0,9,5,0,0,6,0],
[7,0,0,0,9,0,0,0,1],
[0,0,1,2,0,0,3,0,6],
[0,3,0,0,0,0,0,5,9]]
# check_sudoku should return True
hard = [[1,0,0,0,0,7,0,9,0],
[0,3,0,0,2,0,0,0,8],
[0,0,9,6,0,0,5,0,0],
[0,0,5,3,0,0,9,0,0],
[0,1,0,0,8,0,0,0,2],
[6,0,0,0,0,4,0,0,0],
[3,0,0,0,0,0,0,1,0],
[0,4,0,0,0,0,0,0,7],
[0,0,7,0,0,0,3,0,0]]
subg = [[9,8,7,6,5,4,3,2,1],
[8,7,6,5,4,3,2,1,9],
[7,6,5,4,3,2,1,9,8],
[6,5,4,3,2,1,9,8,7],
[5,4,3,2,1,9,8,7,6],
[4,3,2,1,9,8,7,6,5],
[3,2,1,9,8,7,6,5,4],
[2,1,9,8,7,6,5,4,3],
[1,9,8,7,6,5,4,3,2]]
fpgd = [[2,9,0,0,0,0,0,7,0],
[3,0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,0,0,0,0],
[5,0,0,0,0,0,0,7,3],
[0,0,2,0,1,0,0,0,0],
[0,0,0,0,4,0,0,0,9]]
def runAssert():
assert check_sudoku(ill_formed) == None
assert check_sudoku(valid) == True
assert check_sudoku(invalid) == False
assert check_sudoku(easy) == True
assert check_sudoku(hard) == True
assert check_sudoku(subg) == False
assert check_sudoku(fpgd) == None
assert check_sudoku(fpgd2) == None
assert check_sudoku(boolg) == None
assert check_sudoku(tupl) == None
assert check_sudoku(tuplb) == None
assert check_sudoku(worst) == True
def check9(line):
previous=0
for j in range(len(line)):
if previous==0:
# it's ok r[j] == anything
previous=line[j]
else:
if previous==line[j]:
return False
previous = line[j]
return True
def check_sudoku(grid):
###Your code here
if len(grid)!=9:
return None
for i in range(len(grid)):
if len(grid[i])!=9:
return None
if not isinstance(grid, list):
return None
for i in range(len(grid)):
if not isinstance(grid[i], list):
return None
for j in range(len(grid[i])):
if not isinstance( grid[i][j], int):
return None
if grid[i][j] is True or grid[i][j] is False:
return None
for i in range(len(grid)):
#row
r=sorted(grid[i])
if not check9(r):
return False
#column
r=sorted([a[i] for a in grid])
if not check9(r):
return False
#sub-grid
ir=(i/3)*3
ic=(i%3)*3
r=sorted([grid[a][b] for a in range(ir,ir+3) for b in range(ic,ic+3)])
if not check9(r):
return False
return True
#print check_sudoku(ill_formed) # --> None
#print check_sudoku(valid) # --> True
#print check_sudoku(invalid) # --> False
#print check_sudoku(easy) # --> True
#print check_sudoku(hard) # --> True
runAssert()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment