Home > Back-end >  need to make this into a recursive function
need to make this into a recursive function

Time:12-03

i need to convert this over to a recursive function. the program basically prints out all variations of a string.

def comb(L):          
    for i in range(3):
        for j in range(3):
            for k in range(3):
                  
                # check if the indexes are not
                # same
                if (i!=j and j!=k and i!=k):
                    print(L[i], L[j], L[k])
                      
# Driver Code
comb([1, 2, 3])

output:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

CodePudding user response:

This might get you started:

  • Every permutation of a list can be made by picking a value from the list & putting it at the front of every permutation of what is left in the list after taking that value out. (Note that the "what is left" part is a smaller list than what you started with.)
  • When your list is small enough, it is the only permutation.
  • Related