Challenge - 5 Problems
Word Frequency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of word frequency count using Counter
What is the output of this Python code that counts word frequencies in a list?
Data Analysis Python
from collections import Counter words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'] freq = Counter(words) print(freq)
Attempts:
2 left
💡 Hint
Remember that Counter counts how many times each word appears.
✗ Incorrect
Counter creates a dictionary-like object showing counts of each word. 'apple' appears 3 times, 'banana' 2 times, and 'orange' once.
❓ data_output
intermediate1:30remaining
Number of unique words in a text
Given this list of words, how many unique words are there?
Data Analysis Python
words = ['data', 'science', 'data', 'analysis', 'science', 'python', 'python', 'python'] unique_words = len(set(words)) print(unique_words)
Attempts:
2 left
💡 Hint
Use set to find unique items in a list.
✗ Incorrect
The unique words are 'data', 'science', 'analysis', and 'python', so total 4.
❓ visualization
advanced2:30remaining
Bar chart of word frequencies
Which option shows the correct bar chart for the word frequencies in this data?
Data Analysis Python
import matplotlib.pyplot as plt from collections import Counter words = ['cat', 'dog', 'cat', 'bird', 'dog', 'dog'] counter = Counter(words) plt.bar(counter.keys(), counter.values()) plt.show()
Attempts:
2 left
💡 Hint
Count how many times each word appears before plotting.
✗ Incorrect
The word 'dog' appears 3 times, 'cat' 2 times, and 'bird' once, so the bar heights match these counts.
🔧 Debug
advanced2:00remaining
Identify the error in word frequency code
What error does this code raise when counting word frequencies?
Data Analysis Python
words = ['red', 'blue', 'red', 'green'] freq = {} for word in words: freq[word] += 1 print(freq)
Attempts:
2 left
💡 Hint
Check what happens when you try to add to a dictionary key that does not exist yet.
✗ Incorrect
The dictionary freq is empty initially, so freq[word] += 1 tries to add to a missing key, causing KeyError.
🚀 Application
expert3:00remaining
Find the most frequent word in a large text
Given a large text split into words, which code snippet correctly finds the most frequent word?
Data Analysis Python
text = 'data science is fun and data science is powerful and data is everywhere'
words = text.split()Attempts:
2 left
💡 Hint
Use Counter's most_common method to get the word with highest count.
✗ Incorrect
Option A uses Counter and most_common(1) to get the word with highest frequency. Other options either find max key or max count but do not get the word correctly.