Would like to do the following by recursion so that I can vary the number of 'for' loops:
n = 5
out = []
for i in range(n):
for j in range(i,n):
for k in range(j,n):
out.append([i,j,k])
To return
out = [[0 0 0]
[0 0 1]
[0 0 2]
[0 0 3]
[0 0 4]
[0 1 1]
[0 1 2]
[0 1 3]
[0 1 4]
[0 2 2]
[0 2 3]
[0 2 4]
[0 3 3]
[0 3 4]
[0 4 4]
[1 1 1]
[1 1 2]
[1 1 3]
[1 1 4]
[1 2 2]...]
e.g.
def Recurse(n, p):
# where p is the number of for loops
some magic recursion
return out
I've had a look at some of the other recursion questions, but struggling to get to the solution.