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

Saving the results of the recursion.

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/cqsS3oVcQ072/?ref=app

16th Aug 2020, 7:28 PM
27_07b
27_07b - avatar
2 Answers
+ 2
Python def countdown(): i=5 while i > 0: yield i i -= 1 l=[] for i in countdown(): l.append(i) print(l)
18th Aug 2020, 8:55 AM
Ajith
Ajith - 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))
18th Aug 2020, 9:38 AM
27_07b
27_07b - avatar