Saving the results of the recursion. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Saving the results of the recursion.

(Google translate) The task is as follows. It is necessary to iterate over all combinations of numbers from zero to "set - 1" (set = 4, then 0,1,2,3). The length of the combination "length" (If length = 3, there can be such combinations - 000,010,111). The task is trivial, but I have no idea how to keep all these combinations in the list, since the combination ends in the base case. https://code.sololearn.com/cL2Ha6krzc8T/?ref=app

17th Aug 2020, 8:55 AM
27_07b
27_07b - avatar
2 Answers
0
If your only problem is storing: The last thing you type before going deeper in recursion is always something like: result= currentState + thisFunction(someChangedThings) The result is going deeper, but when you hit the end condition, it bubbles up, and you have the result you are associating by the expression "=". In your code, you are just calling the function again, but its not gonna be saved anywhere. You get it?
17th Aug 2020, 9:55 AM
Jiří Zbořil (ZboraCZ)
Jiří Zbořil (ZboraCZ) - avatar
+ 1
I managed to figure out the problem. But thanks anyway. def pr(set,length,list=[],s=[]): if length==0: j=list[:] s.append(j) return for i in range(set): list.append(i) pr(set,length-1,list,s) list.pop() return s print(pr(2,3))
17th Aug 2020, 5:22 PM
27_07b
27_07b - avatar