Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of this question is to write a complete Python program that is given a list of words and finds the anagrams formed by

The purpose of this question is to write a complete Python program that is given a list of words and finds the anagrams formed by the words. The list of words is below:

acres alerted altered arm aspired cares despair diapers drapes east eats esprit except expect filter ideals ladies lameness least lemons lifter limes mar melons miles nameless naps opts padres palest pans pares parse parsed pastel pears petals plates pleats pores poser post pots praised priest prose races ram rasped reaps related reset restrain retrains rites ropes sailed salesman salve sate scare seat skate slate slave slime smile snap solemn span spare spared spear spore spot spread sprite stake stale staple steak steal steer strainer stripe takes tales teaks teals teas terrains terse tiers tires trainers treadle trees tries trifle vales Insert the following statements at the beginning of your program. Later in the course we will learn what each statement does. from sys import path from os import chdir chdir(path[0]) def getWords(filename): """ filename -> list of words from the file / document Given the name of a file return a list of words from the file. """ # filename - the name of the file containing the words # infile - the file descriptor # data - the content of the file as one string infile = open(filename, 'r') data = infile.read() infile.close() return data.split() Write a function that begins with the following header: def sortLettersInWord(word): This function is given a word as a string. It returns a new string that contains the same letters that are in the word with the letters sorted alphabetically. For example, if the given word is 'altered' the function returns 'adeelrt'. A simple way of doing this is to convert the string to a list of letters, sort the list of letters and then convert the sorted list of letters back into a string that is returned by the function. Write a function that begins with the following header: def buildSortedWordsList(words): This function is given the list of words whose letters are to be sorted. For each word in the list of words call sortLettersInWord to get a word whose letters are sorted. Add the sorted words into a list of sorted words. Return the list of sorted words. Write a function that begins with the following header: def buildAnagramLists(words, sortedWords): This function is given: words - the list of words from the file sortedWords - the list of words whose letters have been sorted alphabetically The function is to create a list of the unique words whose letters have been sorted and a list of lists that contains the anagrams of the unique words, these words come from the list of words. For example, 'acres', 'cares', 'racers' and 'scare' form the list of anagrams for the words whose letters have been sorted to be 'acers'. These words are to be added to the list of words for 'acers'. This list is to be a list within the list of lists containing the anagrams. For example, if the lists of unique words whose letters have been sorted is ['acers' ,'amr'] then the list of lists containing the anagrams might be [['acres', 'cares', 'races', 'scare'], ['arm', 'mar', 'ram']]. The list ['acres', 'cares', 'racers', 'scare'] is for 'acers', and the list ['arm', 'mar', 'ram'] is for 'amr'. Return the list of unique words whose letters have been sorted followed by the list of lists of anagrams. Write a function that begins with the following header: def displayAnagrams(uniqueSortedWords, anagramWordLists): This function is given the list of unique words whose letters have been sorted and the list of lists containing the anagrams. The words in these lists are displayed under a heading as shown in the sample run of the program. On each line display the unique word whose letters have been sorted, left justified in 10 character positions, followed by the words that are the anagrams for those letters as shown in the sample run of the program. Write a function that begins with the following header: def main(): This function: inputs the name of the file from the user calls getWords to get the list of words from the file calls buildSortedWordsList to get the list of words whose letters have been sorted calls buildAnagramLists to get the list of unique words whose letters have been sorted and the list of lists containing the anagram words calls displayAnagrams to display the anagrams calls displayTerminationMessage, as defined in assignment 2, to display the termination message. The main program, not to be confused with the function named main, contains all the import statements, the functions and a call to the function named main.

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

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions