MediumDictionariesNot started

Word frequency report

A support team wants a quick summary of labels used in a ticket note. Read lowercase words from one line, count every occurrence, and print each distinct word alphabetically followed by its count.

Input

One line containing 1 to 100 lowercase words separated by single spaces.

Output

One line per distinct word in alphabetical order: word, one space, count.

Example 1

Input

late parcel late address parcel late

Output

address 1
late 3
parcel 2

Counts are accumulated, then keys are printed alphabetically.

Constraints

  • Each word has 1 to 20 lowercase letters
  • At least one word is present

Hints

Hint 1 of 3

Use each word as a dictionary key.

Hint 2 of 3

get(word, 0) provides the starting count.

Hint 3 of 3

Sort the dictionary keys only when printing.

Solution

Show a reference solution and explanation
 Python · reference solution
words = input().split()
counts = {}
for word in words:
    counts[word] = counts.get(word, 0) + 1
for word in sorted(counts):
    print(word, counts[word])

Why it works

The dictionary maps each distinct word to a running count. Updating is constant-time on average, while sorting at the end makes output stable without complicating the counting pass.

Lesson for this exercise: Dictionaries in Python

Your program
words = input().split()
counts = {}
# count every word
for word in sorted(counts):
    print(word, counts[word])

Tests: 3 cases including the examples. Passing every test marks the exercise solved in this browser.

Ready for a challenge?

How this page was checked. Every program on it was run with CPython 3.11 at build time; runs in your browser on CPython 3.14 (Pyodide) by the publishing checks, and the output shown is what it printed. The Run buttons execute the same code on your device; nothing you type is sent anywhere.