Question: 1 2 3 #This time, write a new function called length-words so that #the returned dictionary maps an Integer, the length of a #word, to
1

2

3

#This time, write a new function called length-words so that #the returned dictionary maps an Integer, the length of a #word, to a list of words from the sentence with that length. #If a word occurs more than once, add it more than once. The #words in the list should appear in the same order in which #they appeared in the sentence #For example: # length words ("I ate a bowl of cereal out of a dog bowl today.") #->3: ['ate'"dogOut'], 1: ['a', a,i' 5: 'today'], 2: [of,of], 4: ['bowl', 6: [ cereal'] #As before , you should remove any punctuation and make the #string lowercase. #Hint : To create a new list as the value for a dictionary key, #use empty brackets: lengths [wordLength] - []. Then, you would #be able to call lengths [wordLength].append (word). Note that #if you try to append to the list before creating it for that #key, you'll receive a KeyError. #Write your function here! #Below are some lines of code that will test your function. #You can change the value of the variable (s) to test your #function with different inputs. #If your function works correctly, this will originally #print : #11: ['i', 'a', 'a'], 2: ['of ' , 'of ' ], 3: ['ate', 'out ' , 'today, 6: ['cereal'j) 'dog ' ], 4: ['bowl ' , 'bowl ' j, 5: #The keys may appear in a different order, but within each #list the words should appear in the order shown above. print (length words("I ate a bowl of cereal out of a dog bowl today."))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
