简体   繁体   中英

need to make this into a recursive function

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM