Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

2. Discuss how a business can increase a customers lifetime value.

Answered: 1 week ago