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.

Respuesta :

Answer:

Check the explanation

Explanation:

Executable Code:

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 = []

   # Call the function to create and output

   # the permutations of the list.

   all_permutations(permList, nameList)

#endcode

Following are the program to the given question:

Program Explanation:

  • Defining a method "permutation" that takes two parameters as list type "n_List, p_List".
  • Inside the method defining another method "create_Permutations" take one parameter "n_List".
  • In the inner method, a "n" variable is defined that holds a length of n_List and uses a conditional statement to check and return its value.
  • In the next step, an empty list "p_List" is declared that uses the loop to calculate the combination of "n_List".
  • After calculating the combination the p_List is used to hold the "create_Permutations" method value and use loop call this method recursively and print its value.
  • At the last "n_List" is used to input value and split it by the respective method and "p_List" as an empty list, and calling the method "permutation" by passing parameters.

Program:

def permutation(p_List, n_List):#defining a method permutation that takes two parameter  

  def create_Permutations(n_List):#defining a method create_Permutations that take one parameter

 n = len(n_List)#defining n variable to hold length of n_List

      if n == 0:#defining if block to check n equal to 0

          return []#return empty list

      if n == 1:#defining another if block to check n equal to 1

          return [n_List]#return n_List value

      p_List = []#defining an empty list

      for i in range(n):#defining a loop that calculates combination of list

 

   

Output:

Please find the attached file.

Please find the complete code in the attachment.

Learn more:

brainly.com/question/15750929

Ver imagen codiepienagoya
Ver imagen codiepienagoya
Ver imagen codiepienagoya