Python 数独验证程序列和单元格
我正在编写一个程序,它将获取一个users.txt文件并验证数独。下面是一个文件示例Python 数独验证程序列和单元格,python,python-3.x,Python,Python 3.x,我正在编写一个程序,它将获取一个users.txt文件并验证数独。下面是一个文件示例 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 到目前为止,在我的代码中,我试图让它不检查行,但它没有检查每一行。相反,它将此作为输出生
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
到目前为止,在我的代码中,我试图让它不检查行,但它没有检查每一行。相反,它将此作为输出生成
{' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
true
{' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
true
{' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
true
{' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
true
{' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
true
{' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
true
{' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
true
{' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
true
{' ', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
true
这是我的密码。我还想知道如何让它检查列和单元格
file=input("Enter a filename: ")
fi=open(file,"r")
f=fi.read()
row=f.split("\n")
check=set(row)
print(check)
for n in check:
seen=set(n)
print (seen)
if len(seen)<9:
print ("false")
else:
print ("true")
尝试这种枚举技术,其中我检查行和列的小计为45:
with open('data.txt') as data:
row_total = [0 for i in range(9)]
col_total = [0 for i in range(9)]
for a, b in enumerate(data):
sb_row_total = 0
for c, d in enumerate(b.split()):
sb_row_total += int(d)
col_total[c] += int(d)
row_total[a] = sb_row_total
rows = all(i == 45 for i in row_total)
cols = all(i == 45 for i in col_total)
print(rows and cols)
你要我们向你解释你的代码吗?我的暗示是,程序正在做你让它做的事情,你只需要仔细看看。请注意,这些集合的len>9,包括空元素。rows=listopenfile,r