Write a recursive function called that takes a string of single names separated by spaces and prints out all possible combinations (permutations), each combination on a new line. When the input is:...


Write a recursive function called that takes a string of single names separated by spaces and prints out all possible combinations (permutations), each combination on a new line.


When the input is:


Alice Bob Charlie


then the output is:


Alice Bob Charlie


Alice Charlie Bob


Bob Alice Charlie


Bob Charlie Alice


Charlie Alice Bob


Charlie Bob Alice



Here is my original code that needs to be fixed:



def all_permutations(permList, nameList):

    # TODO: Implement method to create and output all permutations of the list of names.

    if nameList == len(permList) - 1:

        return nameList

    else:

        for x in range(permList, len(nameList)):

            permList[nameList], permList[x] = permList[x], permList[name_List]

            return all_permutations(permList, nameList + 1)

            permList[nameList], permList[x] = permList[x], permList[name_List]





if __name__ == "main":

    nameList = input().split(' ')

    permList = []

    all_permutations(permList, nameList)



Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here