Word Frequency Counter

Easy ⏱ 8 min 74% acceptance ★★★★☆ 4.2
Write a function word_freq(text) that splits text on whitespace (lowercased) and returns a dict mapping each word to how many times it appears.

Examples

Example 1
Input
word_freq('the cat sat on the mat the cat ran')
Output
{'the': 3, 'cat': 2, 'sat': 1, 'on': 1, 'mat': 1, 'ran': 1}
Explanation

Count occurrences of each lowercase word.

Constraints

  • Treat words case-insensitively.
  • Split only on whitespace.

Topics

Dictionariescounting

Companies

GoogleAdobe

Hints

Hint 1

Loop over `text.lower().split()`, using `d[word] = d.get(word, 0) + 1`.

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