Hackbright Code Challenges

Word Count: Solution

Word Count: Solution

Problem

Word Count

Whiteboard

Easier

Concepts

Strings, Dictionaries

Download

word-count-solution.zip


count.py
def word_count(phrase):
    """Count words in a sentence, and print in ascending order."""

    # START SOLUTION

    word_count = {}

    for word in phrase.split():
        word_count[word] = word_count.get(word, 0) + 1

    counts = [(count, word) for word, count in word_count.items()]
    counts.sort()

    for count, word in counts:
        print("%s: %s" % (word, count))