Question
Part 1: Create a function named add_item that takes two parameters, the dictionary being used and the word to lookup (in that order). If the
Part 1:
Create a function named add_item that takes two parameters, the dictionary being used and the word to lookup (in that order).
-
If the word is NOT in the dictionary add it with a count of 1
-
If the word is in the dictionary, update the count accordingly
-
Return the current count (after the add) for the word
Create a function named build_dictionary that takes a list of words (as a parameter) and returns a dictionary that contains a count for each word from the list of words. The function build_dictionary will call add_item for every value in the list.
After you are done, the following should work:
sentence = "the quick brown fox jumped over the dog"
words = sentence.split()
d = build_dictionary(words)
# should be True
print(d["the"] == 2)
print(d["dog"] == sentence.count("dog"))
If you need a refresher on split or count (see the lesson on strings)
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