Python 有效数独棋盘检查器 9x9 验证方块但不显示输出结果
Python Valid Sudoku Board Checker 9x9 validate squares but not showing output results
我一直在执行一项任务,验证一个已经完成的数独谜题,看看它是否正确完成。我已经能够定义函数并能够将代码分成不同的部分以允许代码正常流动。我检查了行、列和子矩阵方块。数独谜题是通过文本文件提供的,可以在 运行 运行程序时读取该文件。当我 运行 程序时,唯一显示的是数独板。我必须做什么才能证明这个谜题是
有效吗?
def main():
fl =('sudoku board.txt')
grids =(is_puzzle(fl))
def is_Puzzle(fl):
with open(fl,'r') as files:
grids = []
lines = files.readlines()
for line in lines:
board = line.split()
wholenums = [int(items) for items in board]
print (wholenums)
grids.append(wholenums)
return grids
def is_puzzle(fl):
if check_rows(fl) and check_columns(fl) and check_squares(fl):
return True
else:
return False
def compare_all(fl):
compare = [1,2,3,4,5,6,7,8,9]
return sorted(lst) `== compare
def check_rows(fl):
for i in grids:
if not grids.compare_all(i):
return False
return True
def check_columns(fl):
for i in range(len(fl)):
contain = []
for j in range(len(fl[0])):
contain.append(fl[j][i])
if not grids.compare_all(hold):
return False
def check_squares(fl):
for i in range(0, 9, 3):
for j in range(0, 9, 3):
nums = fl[i][j:j+3]+fl[i+1][j:j+3]+fl[i+2][j:j+3]
if not grids.compare_all(nums):
return False
return True
main()
您的代码已修复,并按预期执行。除了一些调整之外,您还必须检查 is_puzzle() 函数的 return 值。
def main():
fl = 'sudoku board.txt'
grids = (is_Puzzle(fl))
print()
if is_puzzle(grids):
print('The sudoku is correct!')
else:
print('The sudoku is wrong...')
def is_Puzzle(fl):
with open(fl, 'r') as files:
grids = []
lines = files.readlines()
for line in lines:
board = line.split()
wholenums = [int(items) for items in board]
print(wholenums)
grids.append(wholenums)
return grids
def is_puzzle(fl):
if check_rows(fl) and check_columns(fl) and check_squares(fl):
return True
else:
return False
def compare_all(fl):
compare = [1, 2, 3, 4, 5, 6, 7, 8, 9]
return sorted(fl) == compare
def check_rows(fl):
for i in fl:
if not compare_all(i):
return False
return True
def check_columns(fl):
for i in range(len(fl)):
contain = []
for j in range(len(fl[0])):
contain.append(fl[j][i])
if not compare_all(contain):
return False
return True
def check_squares(fl):
for i in range(0, 9, 3):
for j in range(0, 9, 3):
nums = fl[i][j:j + 3] + fl[i + 1][j:j + 3] + fl[i + 2][j:j + 3]
if not compare_all(nums):
return False
return True
main()
我一直在执行一项任务,验证一个已经完成的数独谜题,看看它是否正确完成。我已经能够定义函数并能够将代码分成不同的部分以允许代码正常流动。我检查了行、列和子矩阵方块。数独谜题是通过文本文件提供的,可以在 运行 运行程序时读取该文件。当我 运行 程序时,唯一显示的是数独板。我必须做什么才能证明这个谜题是 有效吗?
def main():
fl =('sudoku board.txt')
grids =(is_puzzle(fl))
def is_Puzzle(fl):
with open(fl,'r') as files:
grids = []
lines = files.readlines()
for line in lines:
board = line.split()
wholenums = [int(items) for items in board]
print (wholenums)
grids.append(wholenums)
return grids
def is_puzzle(fl):
if check_rows(fl) and check_columns(fl) and check_squares(fl):
return True
else:
return False
def compare_all(fl):
compare = [1,2,3,4,5,6,7,8,9]
return sorted(lst) `== compare
def check_rows(fl):
for i in grids:
if not grids.compare_all(i):
return False
return True
def check_columns(fl):
for i in range(len(fl)):
contain = []
for j in range(len(fl[0])):
contain.append(fl[j][i])
if not grids.compare_all(hold):
return False
def check_squares(fl):
for i in range(0, 9, 3):
for j in range(0, 9, 3):
nums = fl[i][j:j+3]+fl[i+1][j:j+3]+fl[i+2][j:j+3]
if not grids.compare_all(nums):
return False
return True
main()
您的代码已修复,并按预期执行。除了一些调整之外,您还必须检查 is_puzzle() 函数的 return 值。
def main():
fl = 'sudoku board.txt'
grids = (is_Puzzle(fl))
print()
if is_puzzle(grids):
print('The sudoku is correct!')
else:
print('The sudoku is wrong...')
def is_Puzzle(fl):
with open(fl, 'r') as files:
grids = []
lines = files.readlines()
for line in lines:
board = line.split()
wholenums = [int(items) for items in board]
print(wholenums)
grids.append(wholenums)
return grids
def is_puzzle(fl):
if check_rows(fl) and check_columns(fl) and check_squares(fl):
return True
else:
return False
def compare_all(fl):
compare = [1, 2, 3, 4, 5, 6, 7, 8, 9]
return sorted(fl) == compare
def check_rows(fl):
for i in fl:
if not compare_all(i):
return False
return True
def check_columns(fl):
for i in range(len(fl)):
contain = []
for j in range(len(fl[0])):
contain.append(fl[j][i])
if not compare_all(contain):
return False
return True
def check_squares(fl):
for i in range(0, 9, 3):
for j in range(0, 9, 3):
nums = fl[i][j:j + 3] + fl[i + 1][j:j + 3] + fl[i + 2][j:j + 3]
if not compare_all(nums):
return False
return True
main()