Command Palette

Search for a command to run...

Hectal
Phase 17: Interviews and Capstone
Python · Exercise 2 of 6 PreviousNext
Intermediate

Top k frequent words

Write top_words(text, k) that lowercases text, splits it on whitespace, and returns a list of the k most frequent words. Higher counts come first. Words with equal counts are ordered alphabetically.

If there are fewer than k distinct words, return all of them. Empty text gives [].

What Check tests

  1. Highest counts first
  2. Ties are alphabetical
  3. Case is ignored
  4. k larger than the number of words
  5. Empty text

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.