I am relatively new to programming. As the title suggests I require an algorithm that lets me get the same function of a variable nested loop. i.e.
for(..)
{ for(..){
for(..){....}
.
.
}}
The Number of nested loop will vary depending upon user input. What I am trying to achieve is finding all combinations of the numbers (10,9,8,7,6,5,4) Now I have read many. Either I dont understand them fully (I am new to programming world) or It doesnt serve my purpose. I need these combinations later in certain calculations and not just print all the combinations. One way, as I have learnt is to use recursion. I dont understand it fully. I tried to make a recursive function but failed miserably. This is what I want
10 10 10
10 10 9
10 10 8
.
.
.
4 4 4
The number can change (like 10 10 10 10 , 10 10 10 9 .. ) These combinations are to be stored in an array as I need them to manipulate later. Please keep it simple and comment.
Preferred language will be java. Any Language will do. A general algorithm is highly recommended. P.S. This is not a homework.
Thanks to amit. Here is the working code
def findcombinations(array,n,sol,tt=[]):
if (n== 0):
tt.append(sol[:])
return
for x in array:
sol.append(x)
findcombinations(array,n-1,sol,tt)
del sol[-1]
return tt
To call the function use
print(findcombinations([1,2],3,[]))