Problem · Medium · 25 min
The most common words
- Medium
- Hash maps
- Sorting
- Strings
Write top_words(text, count). It returns a list of the count words that appear most often in the text. Each item of the list is a list [word, times].
- A word is a run of letters (
atoz). Any other character ends a word. The case of a letter does not matter: write each word in lower case. - Put the words that appear more often first. If two words appear the same number of times, put the word that comes first in the alphabet first.
- If the text has fewer than
countdifferent words, return all of them. - If
countis 0, or if the text has no words, return an empty list.
Examples
| Call | Result |
|---|---|
top_words('the cat and the hat and the bat', 2) |
[['the', 3], ['and', 2]] |
top_words('b a b a c', 3) |
[['a', 2], ['b', 2], ['c', 1]] |
top_words('', 5) |
[] |
Tasks
2 to do
Task 1, not checked yet. Make
top_wordspass the example tests.Task 2, not checked yet. Make
top_wordspass the hidden tests.
Task 1 of 2
Make top_words pass the example tests.