Question: Apermutation of a string is a string that contains all the same latters as the original but possibly in a different order. For example, all

Apermutation of a string is a string that contains all the same latters as the original but possibly in a different order. For example, all six possible permutations of abc are: abc, acb, bac, bca, cab, the Write a function perms that returns a list of all the permutations of the input string. The returned list of strings should give all the permutations (including the original string) in lexiographic order (i.e., as they might appear in a English language dictionary). The starter code below gives an outline of how to implement the function perms recursively. Modify the lines marked FIX to complete the implementation. Alternatively, if you can think of another way to generate the permutations then you can implement perms any way you like. Your code 1 def perms(s): 2 ' ' 'Return a list of all permutations of the given string. ' ' ' 3 # The base case for the recursion for when s contains a single character 4 if len(s) == 1: 5 return None # FIX: what should the base case return? 6 7 # The general case for the recursion. 8 # Break down the problem of finding permutations for n characters 9 # by calling the 'perms' function on n-1 characters n times. 10 result = 11 for c in s: 12 # FIX: Create a substring 'sub' of 's' to recursively pass to 'perms'? 13 sub = None
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
