Top Words in a Review

Python for AIHard+10 XP+10

Top Words in a Review

You want to know which words a customer used most in a review.

Write top_words(text, n). It returns a list of the n most frequent words, most frequent first.

  • Split the text on spaces and treat words as lowercase ("Good" and "good" are the same word).
  • If two words are used the same number of times, the one that appeared earlier in the text comes first.
  • If there are fewer than n different words, return all of them.
  • Empty text gives back an empty list.

Examples

text = "good food good price"
n = 1
returns ['good']
text = "Nice nice staff"
n = 5
returns ['nice', 'staff']

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run