Back to Practice
#0003
Count Word Frequency
EasyPython12 min10 XP
Problem
Given a sentence, return a dictionary counting lowercase words. Words are separated by spaces. Strip basic punctuation from the beginning and end of each word.
Why This Matters
Frequency maps are everywhere: text analysis, feature engineering, logs, surveys, and classic interview problems.
Function Signature
def word_counts(text):
Examples
Example 1
Inputtext = "ML is fun, ml is useful!"
Output{"ml": 2, "is": 2, "fun": 1, "useful": 1}
"ML" and "ml" count as the same word after lowercasing.
Constraints
- Treat words case-insensitively.
- Strip punctuation characters .,!?:; from word edges.
- Return a normal Python dictionary.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 4 hidden categories
Case and punctuation
Input"ML is fun, ml is useful!"
Expected{"ml": 2, "is": 2, "fun": 1, "useful": 1}
Lowercasing and stripping punctuation prevent duplicate-looking keys.
Empty text
Input""
Expected{}
No words means no counts.
Hidden Test Categories
Repeated punctuationSingle wordMixed capitalizationExtra spaces