Editor

Top Words in a Text

Intermediate · Write the code · from 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

From the lesson: 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

Fifty problems, each with a code editor and real tests. Write your solution, press Check, and see exactly which cases pass. Stuck? Hints reveal one at a time, and the solution unlocks once you have checked at least once. Your code and progress are saved in this browser.

Read the lesson

Write top_words(text, n) that returns the n most common words as (word, count) tuples. Ignore case and punctuation; a word is a run of letters (and apostrophes). Ties keep the order of first appearance.

top_words("The cat and the hat. The END!", 2)  ->  [("the", 3), ("cat", 1)]
def top_words(text, n):
pass
Solution unlocks after your first Check

What Check runs

Your code runs first, then each of these lines. The output must match exactly.

RunsExpected output
top_words("The cat and the hat. The END!", 2)[('the', 3), ('cat', 1)]
top_words("go, go, GO! stop.", 5)[('go', 3), ('stop', 1)]

More practice problems

All Python exercises · 50 practice problems