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

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!