Word Frequency Counter Utility

Medium ⏱ 12 min 57% acceptance ★★★★☆ 4.2
Write a function word_frequencies(text) that returns a dict mapping each lowercase word in text to how many times it appears. Words are separated by whitespace, and punctuation such as ., ,, !, ? should be stripped from each word before counting.

Examples

Example 1
Input
word_frequencies("The cat sat. The cat ran!")
Output
{'the': 2, 'cat': 2, 'sat': 1, 'ran': 1}
Explanation

Case is normalized to lowercase and trailing punctuation is stripped before counting.

Constraints

  • Only the punctuation characters . , ! ? need to be stripped.
  • Splitting is done on whitespace.

Topics

Functionsutility functions

Companies

AmazonGoogleZomato

Hints

Hint 1

Use text.lower().split() to get raw tokens, then word.strip(".,!?") on each.

Hint 2

Build the counts with a plain dict and .get(word, 0) + 1, or collections.Counter.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.