我的代码在以下几行中陷入无限循环:
while i <= len(mylist):
if mylist[i][j] == number:
我已经介入了代码,但仍然不知道如何修复它。我试图解决的问题如下:
定义一个过程check_sudoku, 将方形列表作为输入 表示n x n的列表 数独拼图解决方案并返回布尔值 如果输入有效,则为True 数独并且返回布尔值False 否则。
有效的数独广场满足这些要求 两个属性:
广场的每一列都包含 每个从1到n的整数只有一次。
广场的每一行都包含每一行 整数从1到n恰好一次。
您可以假设输入是方形的并且包含at 至少一行和一列。
我编写的以下代码应该只检查行而不是列。任何关于如何解决它以及我做错了什么的建议都会非常感激,所以我会理解并且不会再犯错误。
def check_sudoku(mylist):
i = 0
j = 0
number = len(mylist)
while i <= len(mylist):
if mylist[i][j] == number:
number = number - 1
j = 0
if number == 0:
i = i + 1
number = len(mylist)
else:
j = j + 1
if number not in list:
break
return False
return True
check_sudoku([[1, 2, 3, 4],
[1, 3, 1, 4],
[3, 1, 2, 3],
[4, 4, 4, 4]])
答案 0 :(得分:1)
我将以您提供的列表为例解释该函数。
发生的事情是:
number = len(mylist)
number = 4
while 0 <= 4
if 1 == 4: // This condition will never be true and therefore doesn't run the code below it
while会再次运行,同样会发生。
答案 1 :(得分:0)
如果你放这行
print("i: " + str(i) + " j: " + str(j))
在While循环开始的地方,你会发现i和j永远不会增加,它会保留在第一个位置。
你必须在更大的if语句后增加i和j