Sort by Value
Completed
Read a line of words and print the 3 most frequent words with their counts, most frequent first (ties alphabetical).
Input
One line.
Output
Up to 3 lines: word count
Example
Input
b a c a b a d d d d
Output
d 4 a 3 b 2
Editor
Python
Hints
counts.items() gives (word, count) pairs.
Sort the pairs with a key and slice the first 3.
Solution explanation
Sorting items() by a custom key is how you order a dict by its values.
Show solution
counts = {}
for w in input().split():
counts[w] = counts.get(w, 0) + 1
for w, c in sorted(counts.items(), key=lambda kv: (-kv[1], kv[0]))[:3]:
print(w, c)