Medium's article tools need to identify the most frequently used words in a paragraph. Given a paragraph and an integer n, return the top n words ranked by descending frequency. Break ties alphabetically.
Words are contiguous sequences of English letters. Matching is case-insensitive, and punctuation is ignored. Return each result as a two-element list containing the lowercase word and its frequency. If n exceeds the number of distinct words, return every distinct word.
paragraph, a non-empty string, and n, a positive integer.[word, frequency] pairs ordered by decreasing frequency, then alphabetically for equal frequencies.def top_n_words(paragraph, n):