Top words

Write top_words(text, k) that returns the k most frequent words in text (split on spaces), most frequent first. When two words have the same count, the alphabetically earlier word comes first. k is at most the number of distinct words.

top_words("the cat sat on the mat the cat ran", 2)  ->  ["the", "cat"]
top_words("b a b a c", 3)                            ->  ["a", "b", "c"]

The point is to practise a Counter feeding a heap of tuples, where the tuple order handles the tie-break for you.

def top_words(text, k):
    ...