Question

# DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from #...

# DISCUSSION SECTION WORK:
#
# 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from
#     http://www.cs.uiowa.edu/~cremer/courses/cs1210/etc/ds4/
#     Save both in the same folder.
#
# 2. TA (aloud) and STUDENTS:  Read the comments from START HERE! (just after these instructions)
#     to definition of anagramInfo function. Discuss any questions about what the functions should do.
#    
# 3. TA demonstrate running anagramInfo("wordsMany.txt") on this unchanged file, to
#         see that it behaves reasonably despite having incomplete anagram-testing functions.
#         STUDENTS should do the same on their computers.
#         Try regular words ("cat", etc.) and non-words (e.g. "abcd"). Quit via Enter/Return.
#
# 4. STUDENTS: Implement areAnagrams, and test it directly.
#         E.g. test via areAnagrams("cat", "act"), areAnagrams("bull", "bulb"), etc.
#
#     BIG HINT (TA DISCUSS THIS HINT IF HELPFUL):
#            How can we test if two words are anagrams?
#                       Type to a Python shell:  sorted("cat") and then sorted("act")
#                        From what you see, you should get an idea of how to
#                        implement areAnagrams very easily.
#   
# 5. STUDENTS: Implement findAnagramsOf and test it. There is a hint in findAnagramsOf stub below.
#
# 6. Finally, try anagramInfo("wordsMany.txt") again.
#         Try on whatever words you want. Some suggestions: art, stop, spear, least
#
#     SUBMIT THIS WHOLE FILE ON ICON.
#

####### START HERE! (AFTER READING INSTRUCTIONS ABOVE) ##########  

#
# Two words are anagrams of each other if it is possible to rearrange
# the letters to produce the other.
# For example, "rat" and "art" and "tar" are anagrams of each other,
# as are "ropes" and "prose"
#
# Note: it is not enough for the two words just to have the same letters.
# E.g., "bull" and "bulb" are *not* anagrams.  They both contain b, u, and l but
# the bull cannot be rearranged to spell bulb.
#
# Your job is to complete two simple functions:
#
# areAnagrams(word1, word2): returns True if word1 and word2 are anagrams, False otherwise
#
# findAnagramsOf(word1, wordList): returns a list of all words in wordList that are
#                               anagrams of word1 (a word is an anagram of itself, so if
#                               word1 is in wordList, it should be included in the result)
#
# Working "stubs" for these are at the bottom of this file.

#
# Two other functions, already complete, are provided for you.
#
#
# anagramInfo(filename): provides an interactive loop for querying about anagrams.
#
# getWordList(filename): given the name of a file of words, return a list containing all the words.
#

#
#. Given the filename of a file of words, this function
#
#  1. first reads all of the words of the file and stores them in a list.
#  2. prints the number of words read
#  3. enters an interactive loop that repeatedly
#      requests the user to type in a word.
#      - If the user types in a word that is in the word list,
#           the function will print a list of the anagrams of that word.
#      - if the user types a word that is not in the word list, a suitable
#        message is printed before requesting input from the user again.
#      - if the user presses only Enter/Return, the loop terminates and the
#        function returns.
#
# (DO NOT MODIFY THIS!)
#
def anagramInfo(filename):

    wordList = getWordList(filename)
    print("Read {} words from file '{}'.".format(len(wordList), filename))
    print("Now you can ask for the anagrams of any word you like.")
    print("(hit Return/Enter when you want to quit)")
    print()
    query = input("What word do you want to know about? ")
    while query != '':
        if query in wordList:
            anagramList = findAnagramsOf(query, wordList)
            print(anagramList)
        else:
            print("'{}' is not in the word list. Please try again.".format(query))
        #
        query = input("What word do you want to know about? ")

    print("Goodbye!")

#
#  given the name of a file of words, return a list containing all the words
#  (DO NOT MODIFY!)
#
def getWordList(filename):
    result = []
    fileStream = open(filename, 'r')
    for line in fileStream:
        word = line.strip()
        result.append(word)
    return result

# return True if word1 and word2 are anagrams, False otherwise
#
def areAnagrams(word1, word2):
    # MODIFY THIS
    # HINT: use Python's sorted function
    return False

#
#  returns a list of all words in wordList that are anagrams of word1
#  Note: a word is an anagram of itself so if word1 is in wordList,
#    it should be included in the result
#

def findAnagramsOf(word1, wordList):
    result = []
    # MODIFY THIS
    # HINT: you just need a simple loop that goes through word list, adding items
    #   to result if they are anagrams of word1
    return result







        

            
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello. Hope this answer helps you. Thank you.

I have modified only the functions mentioned in the question and attaching screenshot here

Please observe the last line that calls the actual function. Please include it in the file so that the program runs.

Sample Input output:

Read 113809 words from file 'wordsMany.txt'.
Now you can ask for the anagrams of any word you like.
(hit Return/Enter when you want to quit)

What word do you want to know about? rat
['art', 'rat', 'tar']
What word do you want to know about? cheat
['cheat', 'tache', 'teach', 'theca']
What word do you want to know about? wheat
['wheat']
What word do you want to know about? anagram
['anagram']
What word do you want to know about? gram
['gram']
What word do you want to know about? rag
['gar', 'rag']
What word do you want to know about?
Goodbye!

Add a comment
Know the answer?
Add Answer to:
# DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from #...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from...

    Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from a set of text files at startup. The filenames are nouns.txt, verbs. txt, articles.txt, and prepositions.txt. (HINT: Define a single new function, getWords. This function should expect a filename as an argument. The function should open an input file with this name, define a temporary list, read words from the file, and add them to the list. The function should then convert the list...

  • ***How do I insert the Halloween text into this and write the program**** Topics: List, tuple...

    ***How do I insert the Halloween text into this and write the program**** Topics: List, tuple In this lab, you will write a scrambled word game.  The game starts by loading a file containing scrambled word-answer pair separated.  Sample of the file content is shown below.  Once the pairs are loaded, it randomly picks a scrambled word and has the player guess it.  The number of guesses is unlimited.  When the user guesses the correct answer, it asks the user if he/she wants another scrambled...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything...

    PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything a course uses in a grading scheme, # like a test or an assignment. It has a score, which is assessed by # an instructor, and a maximum value, set by the instructor, and a weight, # which defines how much the item counts towards a final grade. def __init__(self, weight, scored=None, out_of=None): """ Purpose: Initialize the GradeItem object. Preconditions: :param weight: the weight...

  • how do I write this code without the imports? I don't know what pickle is or...

    how do I write this code without the imports? I don't know what pickle is or os.path import pickle # to save and load history (as binary objects) import os.path #to check if file exists # character value mapping values = {'A': 1, 'B': 3, 'C': 3, 'D': 2, 'E': 1, 'F': 4, 'G': 2, 'H': 4, 'I': 1, 'J': 8, 'K': 5, 'L': 1, 'M': 3, 'N': 1, 'O': 1, 'P': 3, 'Q': 10, 'R': 1, ' S': 1,...

  • My Python file will not work below and I am not sure why, please help me...

    My Python file will not work below and I am not sure why, please help me debug! ********************************* Instructions for program: You’ll use these functions to put together a program that does the following: Gives the user sentences to type, until they type DONE and then the test is over. Counts the number of seconds from when the user begins to when the test is over. Counts and reports: The total number of words the user typed, and how many...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT