Skip to contentKEEP THE CRAFTTHE SANCTUARY

Rank Top Words

Sort words by count, high to low, break ties alphabetically, and keep the first few.

The problem

Implement top_words(counts, limit). counts is a dictionary that maps each word to how many times it appeared. Return a list of at most limit words, ordered from the highest count to the lowest. When two words have the same count, put them in alphabetical order. If there are fewer than limit words, return all of them. For example, with {"beta": 2, "alpha": 2, "gamma": 1} and limit 2 the answer is ["alpha", "beta"].

Starter code

def top_words(counts, limit):
    pass

Public examples

  1. counts = {"beta": 2, "alpha": 2, "gamma": 1}, limit = 2gives["alpha", "beta"]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.