This has been graded as incorrect. Could anyone tell me what is wrong with this answer? (If posting this code is not appropriate I will delete it). Thank you!
#THREE GOLD STARS
#Sudoku [http://en.wikipedia.org/wiki/Sudoku]
#is a logic puzzle where a game
#is defined by a partially filled
#9 x 9 square of digits where each square
#contains one of the digits 1,2,3,4,5,6,7,8,9.
#For this question we will generalize
#and simplify the game.
#Define a procedure, check_sudoku,
#that takes as input a square list
#of lists representing an n x n
#sudoku puzzle solution and returns
#True if the input is a valid
#sudoku square and returns False
#otherwise.
#A valid sudoku square satisfies these
#two properties:
# 1. Each column of the square contains
# each of the numbers from 1 to n exactly once.
# 2. Each row of the square contains each
# of the numbers from 1 to n exactly once.
correct = [[1,2,3],
[2,3,1],
[3,1,2]]
incorrect = [[1,2,3,4],
[2,4,1,3],
[4,1,2,3],
[1,2,3,4]]
def check_sudoku(list):
row = []
column = []
counter = 0
while counter < len(list):
for x in list:
if x[counter] in column:
return False
else:
column.append(x[counter])
column = []
counter = counter + 1
for x in list:
for y in x:
if y in row:
return False
else:
row.append(y)
row = []
return True
print check_sudoku(correct)
print check_sudoku(incorrect)
asked
14 Mar '12, 19:27
Tadeusz Jordan
148●1●1●18
accept rate:
85%