+ 2

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. To make that I've created a simpler version that makes something different, this code takes two integer numbers ci and 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. 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. 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: {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. 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. I don't really know what's going on. I would appreciate your help. Here is the code: https://code.sololearn.com/cMacM2x66JD6

14th Feb 2021, 6:46 PM
Krys
Krys - avatar
5 ответов
+ 5
I don't understand at all what it is suppose to do but actually the program is outputting in second one but it is stuck in an infinite(probably) loop , you should make use of print statements to see if you are really getting what you want. For example in the following code , for i in main_level: print(i,cf) if i==cf: break else: print("hey",i) steps+=1 lower_level(main_level) for the first 2 numbers 0 and 6 the else part is executed , so i don't get why were you even expecting it to output 1. when for 0 lower_level is called it stucks (probably in an infinite loop) , and which makes it quite hard for me to understand anything at all (more hard as i don't even know anything about it) , just use print statements to print out the few sett value for every " i in config_list" and after the for loop completes .
14th Feb 2021, 9:02 PM
Abhay
Abhay - avatar
+ 4
The problem is solved !!, as you mentioned, the for loop in 'check_main_level' function wasn't well defined; I changed it by: var = False for i in config_list: if i==cf: var=True if not var: steps+=1 lower_level(config_list) And it works perfectly; thanks.
14th Feb 2021, 9:52 PM
Krys
Krys - avatar
+ 3
Great! But i don't get where you have done this change , config_list is defined inside lower_level function only !
14th Feb 2021, 10:06 PM
Abhay
Abhay - avatar
+ 2
This is a very hard problem!I think you have to define distance as a class not simple function.
14th Feb 2021, 8:38 PM
HBhZ_C
HBhZ_C - avatar
+ 1
Sorry; I didn't actualized the code; now it is, I have eliminate the global variable main_level, so I make reference only to config_list parameter...On the other hand, the code for the Flip Cylinder is in GitHub if you want to check it :D.
15th Feb 2021, 4:45 AM
Krys
Krys - avatar