Question: Python Length Distribution (3 pts) In this problem, we will test your work in 2 ways: (i) as standalone functions, get_words (0.5 pts) and get_histogram

Python  Python Length Distribution (3 pts) In this problem, we will testyour work in 2 ways: (i) as standalone functions, get_words (0.5 pts)

Length Distribution (3 pts) In this problem, we will test your work in 2 ways: (i) as standalone functions, get_words (0.5 pts) and get_histogram (1.5 points); and (ii) correct functionality when sending returned values from get_words to get_histogram (1 point). get_words(filename) Given a filename as a string, complete the implementation of this function given in the starter code to open the file and retrieve words from each line to create and return) a list with whitespaces stripped. Hints: The str.strip() method returns a copy of the string with the leading and trailing characters removed, whitespace by default. Input filename A valid file name as a Python string) of a .txt file saved in working directory Output list A list with the sequence of all the words contained in filename Example: .txt file for this doctest is available on Canvas and must be saved in the same directory as your .py file >>> get_words('contents.txt') ['week', 'bat', 'aquatic', 'eggs', 'threatening', 'crash', 'educated', 'adjoining', 'bent', 'mice', 'belief', 'adjustment', 'blood', 'smooth', 'kaput', 'mountain', 'digestion', 'enchanted', 'wandering', 'fresh'] get_histogram(words) Takes a Python list words and returns a dictionary where the key is the length of a word and the value is the count of how many words in the list are of that length, in other words, a histogram dictionary mapping length to word counts. Preconditions: While the function will receive lists, you should not make any assumptions about the size of words. Input words A Python list Output dict A dictionary mapping length to word counts Example: >>> get_histogram(['hello', 'there', 'spring', 'is', 'here']) {5: 2, 6:1, 2:1, 4: 1} >>> list_of_words = get_words('contents.txt') >>> get_histogram(list_of_words) {4: 4, 3:1, 7: 1, 11:1, 5: 4, 8: 2, 9: 4, 6: 2, 10:1}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!