# Define a procedure, check_sudoku,
# that takes as input a square list
# of lists representing an n x n
# sudoku puzzle solution and returns the boolean
# True if the input is a valid
# sudoku square and returns the boolean False
# otherwise.
# A valid sudoku square satisfies these
# two properties:
# 1. Each column of the square contains
# each of the whole numbers from 1 to n exactly once.
# 2. Each row of the square contains each
# of the whole numbers from 1 to n exactly once.
# You may assume the the input is square and contains at
# least one row and column.
def rowcheck(l):
sign=1
for i in l:
original=[1,2,3,4,5,6,7,8,9]
p=original[:len(l)]
for j in p:
if j not in i:
sign=-1
break
else:
pos = i.index(j)
k=i[pos+1:]
if j in k:
sign=-1
break
if sign==1:
return True
else:
return False
def colcheck(l):
i=0
p=l+[1,3,3]
while i<len(l):
j=0
while j<len(l):
l[i][j]=p[j][i]
j=j+1
i=i+1
return rowcheck(l)
def check_sudoku(a):
if rowcheck(a)==True and colcheck(a)==True:
return True
else:
return False
check_sudoku
最新推荐文章于 2020-07-20 11:58:52 发布