Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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. .
-
- 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 below to a script that defines the method get_permutations. - Compare the words in the Unix dictionary to the permutation strings in the array. Print any matches that are found. Here is some pseudocode that might help you:
Define get_permutations method # Start main script. # Indicate in a comment the Jumble strings you # use for testing. Include at least two test strings. Input Jumble string from keyboard. Obtain array of permutations from array using get_permutations method. Open Unix dictionary file to obtain file object fin. Get first line of dictionary file. while line not equal to "" Get word from current line by removing at end of line. if word[0] is equal to word[0] converted to uppercase for each permutation in array called perm if perm is equal to word print word exit script (optional) end end end Get next line of dictionary file. end Close file object fin.
The reason we only consider words that start with lower case letters is that Jumble strings never represent proper names.
- 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:
Grading Breakdown: Functionality: 65%; Test strings included in a comment: 10%; Source code comments: 10%; Indentation: 10%; Properly Submitted: 5%.
Comments, Hints, and Observations
- Here are some information from found using the search string "Python permutations of list:"
- The URL https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-18.php suggests this source code for finding all of the permutations of the list [1, 2, 3]:
import itertools print(list(itertools.permutations([1,2,3]))) # Output: [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
We just have to convert our input string to a list of letters before running it through itertools.permutations:import itertools input_string = input("Enter an input string: ") letters = list(input_string) permutation_list = itertools.permutations(letters) for perm in permutation_list: print perm # Output: Enter an input string: dog ('d', 'o', 'g') ('d', 'g', 'o') ('o', 'd', 'g') ('o', 'g', 'd') ('g', 'd', 'o') ('g', 'o', 'd')
We just have to convert each of the permutations to a string.
- The URL https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-18.php suggests this source code for finding all of the permutations of the list [1, 2, 3]:
- Here is a website that showed how to do this: https://www.geeksforgeeks.org/python-permutation-given-string-using-inbuilt-function/:
# Function to find permutations of # a given string from itertools import permutations def allPermutations(str): # Get all permutations of string 'ABC' permList = permutations(str) # print all permutations for perm in list(permList): print(''.join(perm)) # Driver program if __name__ == "__main__": str = 'ABC' allPermutations(str)
The missing piece isfor perm in list(permList): print (''.join(perm))
which we can combine with the code from item to obtainimport itertools input_string = input("Enter an input string: ") letters = list(input_string) permutation_list = itertools.permutations(letters) for perm in permutation_list: print(''.join(perm)) # Output: Enter an input string: dog dog dgo odg ogd gdo god
Finally, we don't want to print all of the permutations, but instead enter them into a list of strings. Here is code to accomplish this:# Source code for obtaining permutations import itertools input_string = input("Enter an input string: ") letters = list(input_string) permutation_list = itertools.permutations(letters) permutations = [ ] for perm in permutation_list: permutations.append(''.join(perm)) print(permulations)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started