1. Count words
EasyWrite a function count_words(text) that counts how often each word appears in a text. Upper and lower case don't matter.
Example: count_words("The dog chased the ball") returns {"the": 2, "dog": 1, "chased": 1, "ball": 1}
Tip: text.lower().split() gives you the words, a dictionary keeps the count.
Show solution
def count_words(text):
counts = {}
for word in text.lower().split():
counts[word] = counts.get(word, 0) + 1
return counts
Shorter with collections.Counter: Counter(text.lower().split()).