Lesson guide
What this Python exercise practices
Count word frequencies in a text is a intermediate practice lesson that focuses on functions, parameters, return values. It is designed to be solved in about 18 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Function parameters
- Return values
Difficulty and time
- Level
- Intermediate
- Estimated time
- 18 minutes
Practice path
Summary
Parse a text and count how often each word appears, normalizing case and ignoring punctuation.
Problem statement
Given an input string, return a dictionary where keys are words (normalized to lowercase) and values are counts of how many times each word appears. Words should be sequences of alphanumeric characters and underscores (use word boundaries). Punctuation and other non-word characters should be ignored. Preserve the order of first appearance of each distinct word in the text when constructing the resulting dictionary.
Task
Create a function that returns a dictionary mapping words to their occurrence counts in a given text. Treat words case-insensitively and ignore punctuation.
Examples
Simple case-insensitive count
Input
count_word_frequencies('Hello world hello')
Output
{'hello': 2, 'world': 1}
Explanation
The function lowercases words and counts occurrences: 'hello' appears twice, 'world' once. The first appearance order is 'hello', then 'world'.
Input format
A single string containing the text to analyze. Example: "This is a test."
Output format
A dictionary mapping normalized lowercase words to integer counts.
Constraints
- Use only Python standard library. - Treat words as sequences matched by the regex \b\w+\b (letters, digits, underscores). - Return an empty dictionary for empty or punctuation-only strings. - Preserve first-seen word insertion order in the result.
Samples
Sample input 0
count_word_frequencies("It's a test. A test!")
Sample output 0
{'it': 1, 's': 1, 'a': 2, 'test': 2}
Explanation 0
Tokenization splits "It's" into 'it' and 's' using the word regex; all words are lowercased and counted.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.