Question

PYTHON: 14.7 LAB: All permutations of names PLEASE ANSWER IN PYTHON Write a program that lists...

PYTHON: 14.7 LAB: All permutations of names

PLEASE ANSWER IN PYTHON

Write a program that lists all ways people can line up for a photo (all permutations of a list of strings). The program will read a list of one word names, then use a recursive method to create and output all possible orderings of those names, one ordering per line.

NAES MUST BE ALPHABETICAL ORDER AS IN THE EXAMPLE

When the input is:

Julia Lucas Mia

then the output is (must match the below ordering):

Julia Lucas Mia 
Julia Mia Lucas
Lucas Julia Mia
Lucas Mia Julia
Mia Julia Lucas
Mia Lucas Julia
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: The code is for Python 3. Please indent the code as shown in the screenshots.

Program screenshots:

Sample output:

Code to copy:

# Define the function to compute and display

# all the permutations of the given list.

def all_permutations(permList, nameList):

    # Define the function to create a list

    # of all the permutations.

    def createPermutationsList(nameList):

        # Compute and store the length of the list.

        n = len(nameList)

        # Return an empty list if the size is 0.

        if n == 0:

            return []

        # Return the element if the size is 1.

        if n == 1:

            return [nameList]

        # Create an empty list to store the permutations.

        permList = []

        # Start the loop to traverse the list.

        for i in range(n):

            # Store the first element of the current list.

            first = nameList[i]

            # Compute the store the remaining list.

            remaining = nameList[:i] + nameList[i+1:]

            # Start the loop and call the function recursively

            # with the remaining list.

            for perm in createPermutationsList(remaining):

                # Append the element in the permutation list.

                permList.append([first] + perm)

        # Return the permutation list.

        return permList

    # Call the function to compute the permutation list

    # and store the result.

    permList = createPermutationsList(nameList)

    # Start the loop to display the values.

    for perm in permList:

        for val in perm:

            print(val, end = " ")

        print()

# Call the main() function.

if __name__ == "__main__":

    # Prompt the user to enter the input.

    nameList = input().split(' ')

    permList = list()

    # Call the function to create and output

    # the permutations of the list.

    all_permutations(permList, nameList)

Add a comment
Know the answer?
Add Answer to:
PYTHON: 14.7 LAB: All permutations of names PLEASE ANSWER IN PYTHON Write a program that lists...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Lab

    Write a program that lists all ways people can line up for a photo (all permutations of a list of strings). The program will read a list of one word names, then use a recursive method to create and output all possible orderings of those names, one ordering per line.When the input is:Julia Lucas Miathen the output is (must match the below ordering):Julia Lucas Mia  Julia Mia Lucas Lucas Julia Mia Lucas Mia Julia Mia Julia Lucas Mia Lucas Julia

  • 1. Write a program to input a list of names (strings) from the user and store...

    1. Write a program to input a list of names (strings) from the user and store them in an ArrayList. The input can be terminated by entering the empty string or by entering the string “quit”. 2. Add further functionality to your program so that it searches the ArrayList to find the first string and the last string according to dictionary ordering and then prints out these strings (names). Do this exercise without sorting the names in the ArrayList. For...

  • Python 3 coding with AWS/Ide. Objective: The purpose of this lab is for you to become familiar with Python’s built-in te...

    Python 3 coding with AWS/Ide. Objective: The purpose of this lab is for you to become familiar with Python’s built-in text container -- class str-- and lists containing multiple strings. One of the advantages of the str class is that, to the programmer, strings in your code may be treated in a manner that is similar to how numbers are treated. Just like ints, floats, a string object (i.e., variable) may be initialized with a literal value or the contents...

  • Python • Write a program that asks for the names of all of the classes you...

    Python • Write a program that asks for the names of all of the classes you are taking this semester • Save these class names in a list • Print all the items in the list, one per line

  • 7.13 LAB: Word frequencies (lists) Write a program that first reads in the name of an...

    7.13 LAB: Word frequencies (lists) Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method. The file contains a list of words separated by commas. Your program should output the words and their frequencies (the number of times each word appears in the file) without any duplicates. Ex: If the input is: input1.csv and the contents of input1.csv are: hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy the output is: hello 1 cat 2 man...

  • Python please For this program, we will be making lists for the local Zoo. You have...

    Python please For this program, we will be making lists for the local Zoo. You have been provided with 4 lists of data already created for you. Using this data, we update the data at this Zoo. YOU MUST USE LIST FUNCTIONS FOR REMOVING TO RECEIVE YOUR FULL POINTS! Input: A type of animal -- This information should be provided in response the question: "What animal are we removing?" A number corresponding to the selection of a menu number (see...

  • Goal:   Unscramble permuted words by generating all permutations of Jumble string and searching for a word...

    Goal:   Unscramble permuted words by generating all permutations of Jumble string and searching for a word in Unix dictionary. Unix Dictionary: dict.txt Details: Write a method called get_permutations that inputs a string like "dog". Your method should return an array of all permutations of the Jumble string. . For example: s = "dog" perms = get_permutations(a) print(perms) Output: ['dog', 'dgo', 'odg', 'ogd', 'gdo', 'god'] Rewrite the script for obtaining permutations and the end of the Comments, Hints, and Observersions section...

  • Python Implement a program that requests from the user a list of words (i.e., strings) and...

    Python Implement a program that requests from the user a list of words (i.e., strings) and then prints on the screen, one per line, all four-letter strings in the list. >>> Enter word list: ['stop', 'desktop', 'top', 'post'] stop post

  • 1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat,...

    1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat, a recursive function that takes two alphabetically-ordered strings and merges them, leaving the letters in alphabetical order. Note that the strings may be different lengths. a main method that inputs two ordered strings and calls the orderedConcat method to return a resulting merged, ordered string. Your main method should print the two input strings and the result. Note: You may not use any of...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT