For 循环卡在 Python
For cycle gets stuck in Python
我下面的代码卡在了一个随机点上:
import functions
from itertools import product
from random import randrange
values = {}
tables = {}
letters = "abcdefghi"
nums = "123456789"
for x in product(letters, nums): #unnecessary
values[x[0] + x[1]] = 0
for x in product(nums, letters): #unnecessary
tables[x[0] + x[1]] = 0
for line_cnt in range(1,10):
for column_cnt in range(1,10):
num = randrange(1,10)
table_cnt = functions.which_table(line_cnt, column_cnt) #Returns a number identifying the table considered
#gets the values already in the line and column and table considered
line = [y for x,y in values.items() if x.startswith(letters[line_cnt-1])]
column = [y for x,y in values.items() if x.endswith(nums[column_cnt-1])]
table = [x for x,y in tables.items() if x.startswith(str(table_cnt))]
#if num is not contained in any of these then it's acceptable, otherwise find another number
while num in line or num in column or num in table:
num = randrange(1,10)
values[letters[line_cnt-1] + nums[column_cnt-1]] = num #Assign the number to the values dictionary
print(line_cnt) #debug
print(sorted(values)) #debug
如您所见,这是一个使用 2 个字典生成随机数独方案的程序:包含完整方案的值和包含每个 table.[=13= 值的 tables ]
示例:
5th square on the first line = 3
|
v
values["a5"] = 3
tables["2b"] = 3
那么问题是什么?我错过了什么吗?
import functions
...
table_cnt = functions.which_table(line_cnt, column_cnt) #Returns a number identifying the table considered
当我们可以在我们自己的计算机上直接执行代码进行测试时,这真是太好了。换句话说,如果将 "table_cnt" 替换为示例的固定值会很好(这里,一个简单的字符串就足够了)。
for x in product(letters, nums):
values[x[0] + x[1]] = 0
没那么重要,但这样更优雅:
values = {x+y: 0 for x, y in product(letters, nums)}
现在,问题的核心:
while num in line or num in column or num in table:
num = randrange(1,10)
这是你永远循环的地方。因此,您正在尝试生成随机数独。从您的代码中,这就是您生成随机列表的方式:
nums = []
for _ in range(9):
num = randrange(1, 10)
while num in nums:
num = randrange(1, 10)
nums.append(num)
这种方法的问题是您不知道程序需要多长时间才能完成。这可能需要一秒钟或一年(尽管 不太可能)。这是因为无法保证该程序不会一遍又一遍地选择已经使用的号码。
不过,在实践中它仍然需要相对较短的时间才能完成(这种方法效率不高,但列表很短)。但是,对于数独游戏,您可能会陷入不可能的境地。例如:
line = [6, 9, 1, 2, 3, 4, 5, 8, 0]
column = [0, 0, 0, 0, 7, 0, 0, 0, 0]
那些是第一行(或实际上任何一行)和最后一列。当算法尝试为行 [8] 查找值时,它总是会失败,因为 7 被 column
.
阻塞
如果你想保持这种状态(又名蛮力),你应该检测到这种情况并重新开始。同样,这非常低效,您应该看看如何正确生成数独(我天真的方法是从已解决的数独开始并随机交换行和列,但我知道这不是一个好方法)。
我下面的代码卡在了一个随机点上:
import functions
from itertools import product
from random import randrange
values = {}
tables = {}
letters = "abcdefghi"
nums = "123456789"
for x in product(letters, nums): #unnecessary
values[x[0] + x[1]] = 0
for x in product(nums, letters): #unnecessary
tables[x[0] + x[1]] = 0
for line_cnt in range(1,10):
for column_cnt in range(1,10):
num = randrange(1,10)
table_cnt = functions.which_table(line_cnt, column_cnt) #Returns a number identifying the table considered
#gets the values already in the line and column and table considered
line = [y for x,y in values.items() if x.startswith(letters[line_cnt-1])]
column = [y for x,y in values.items() if x.endswith(nums[column_cnt-1])]
table = [x for x,y in tables.items() if x.startswith(str(table_cnt))]
#if num is not contained in any of these then it's acceptable, otherwise find another number
while num in line or num in column or num in table:
num = randrange(1,10)
values[letters[line_cnt-1] + nums[column_cnt-1]] = num #Assign the number to the values dictionary
print(line_cnt) #debug
print(sorted(values)) #debug
如您所见,这是一个使用 2 个字典生成随机数独方案的程序:包含完整方案的值和包含每个 table.[=13= 值的 tables ]
示例:
5th square on the first line = 3
|
v
values["a5"] = 3
tables["2b"] = 3
那么问题是什么?我错过了什么吗?
import functions
...
table_cnt = functions.which_table(line_cnt, column_cnt) #Returns a number identifying the table considered
当我们可以在我们自己的计算机上直接执行代码进行测试时,这真是太好了。换句话说,如果将 "table_cnt" 替换为示例的固定值会很好(这里,一个简单的字符串就足够了)。
for x in product(letters, nums):
values[x[0] + x[1]] = 0
没那么重要,但这样更优雅:
values = {x+y: 0 for x, y in product(letters, nums)}
现在,问题的核心:
while num in line or num in column or num in table:
num = randrange(1,10)
这是你永远循环的地方。因此,您正在尝试生成随机数独。从您的代码中,这就是您生成随机列表的方式:
nums = []
for _ in range(9):
num = randrange(1, 10)
while num in nums:
num = randrange(1, 10)
nums.append(num)
这种方法的问题是您不知道程序需要多长时间才能完成。这可能需要一秒钟或一年(尽管 不太可能)。这是因为无法保证该程序不会一遍又一遍地选择已经使用的号码。
不过,在实践中它仍然需要相对较短的时间才能完成(这种方法效率不高,但列表很短)。但是,对于数独游戏,您可能会陷入不可能的境地。例如:
line = [6, 9, 1, 2, 3, 4, 5, 8, 0]
column = [0, 0, 0, 0, 7, 0, 0, 0, 0]
那些是第一行(或实际上任何一行)和最后一列。当算法尝试为行 [8] 查找值时,它总是会失败,因为 7 被 column
.
如果你想保持这种状态(又名蛮力),你应该检测到这种情况并重新开始。同样,这非常低效,您应该看看如何正确生成数独(我天真的方法是从已解决的数独开始并随机交换行和列,但我知道这不是一个好方法)。