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 perm

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
icon
Related questions
Question

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)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning