[英]Nothing is output in python recursive function
I'm working on a code that calculates the 'distance' between two configurations of a Flip Cube, The distance between two configurations x and y is the minimum number of steps required to go from x to y, or conversely.我正在编写一个代码来计算翻转立方体的两个配置之间的“距离”,两个配置 x 和 y 之间的距离是 go 从 x 到 y 所需的最小步数,或者相反。
To make that I've created a simpler version that makes something different, this code takes two integer numbers ci and cf .为了做到这一点,我创建了一个更简单的版本,使一些不同,这段代码需要两个 integer 数字ci和cf 。 with ci returns an iterable called main_level through the generator called multi , then, it iterates through it searching for the parameter cf , whenever cf is not in main_level the variable steps is increased by 1 and for each element in main_level we repeat the same process done for ci . with ci通过名为multi的生成器返回一个名为main_level的可迭代对象,然后,它遍历它以搜索参数cf ,只要cf不在main_level中,变量steps就会增加 1 并且对于main_level中的每个元素,我们重复相同的过程为ci 。 Finally, when cii==cf the program ends and returns the steps variable, which counts the number of "levels" that we have to go down to find the given parameter cf .最后,当cii==cf程序结束并返回steps变量,它计算我们必须向下 go 找到给定参数cf的“级别”数。 This code doesn't have any practical purpose is just a base for the problem I mentioned above.这段代码没有任何实际用途,只是我上面提到的问题的基础。
If I call the distance(ci, cf) function with ci=5, the first two levels are:如果我用 ci=5 调用距离(ci,cf) function,前两个级别是:
{0,3,6,9,12} <-- first level (steps is initialized with 1) if cf is any of the numbers in the set, the program should end and return steps= 1 , if cf is not in that set, the programs form the second level: {15,18,21,24,27,30,33} and search cf , if cf is there, the program ends and should return steps= 2 , if not, it forms the third level, and so on. {0,3,6,9,12} <-- 第一级(steps 初始化为 1)如果cf是集合中的任何数字,则程序应该结束并返回 steps= 1 ,如果cf不在其中设置,程序形成第二级: {15,18,21,24,27,30,33}并搜索cf ,如果cf在那里,程序结束并应该返回 steps= 2 ,如果没有,它 forms 第三水平等等。 But there is a problem, actually, when I call the distance function with ci =5 and cf = any natural number, and print its value, anything is output, only for cf =0, it outputs step= 1 .但是有一个问题,实际上,当我用ci =5 和cf = 任何自然数调用距离 function 并打印它的值时,任何东西都是 output,仅对于cf =0,它输出 step= 1 。 I don't really know what's going on.我真的不知道发生了什么事。 I would appreciate your help.我会很感激你的帮助。
Here is the code:这是代码:
#Base solution to FlipCube problem
def multi(par):
for i in range(par):
yield i*3
steps=1
def distance(ci,cf):
main_level =set(multi(ci))
global steps
def check_main_level(cf):
global steps
nonlocal main_level
def lower_level(config_list):
sett=set()
for i in config_list:
sett.update(q for q in multi(i) if q not in config_list)
nonlocal main_level
main_level=sett
check_main_level(cf)
for i in main_level:
if i==cf:
break
else:
steps+=1
lower_level(main_level)
check_main_level(cf)
return steps
#testing
e= distance(5,0)
print(e)# prints 1, very good
e2= distance(5,9)
print(e2)# should print 1, but doesn't print anything :(
e3= distance(5,27)
print(e3)# should print 2, but doesn't print anything :(
The program does not terminate recursion under all circumstances.程序不会在所有情况下终止递归。 The culprit seems to be the for
loop in check_main_level
.罪魁祸首似乎是check_main_level
中的for
循环。 Change the code after your definition of lower_level
to:将lower_level
定义后的代码更改为:
# code portion of check_main_level
if cf > max(main_level):
steps+=1
lower_level(main_level)
# end of code portion check_main_level (replacing for-loop)
you have an infinity loop, that's why nothing is printed.你有一个无限循环,这就是为什么没有打印任何内容。
You can see it easyly by adding a print:您可以通过添加打印轻松地看到它:
for i in config_list:
print(i)
sett=set()
sett.update(q for q in list(multi(i)) if q not in config_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.