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
2 Antworten
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?
+ 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))