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
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