Intermediate · Dictionaries · 134 of 264
Letter chart
Count the letters in a line (ignore capitals, spaces and punctuation) and draw a bar of stars for each letter, in alphabetical order.
Input: one line. Output: lines like a: ***
Examples
Input
Hello World
Output
d: * e: * h: * l: *** o: ** r: * w: *
Input
banana
Output
a: *** b: * n: **
Input
abc cba!
Output
a: ** b: ** c: **
One more test checks other inputs when you press Check.
Stuck? The idea behind dictionaries
A dictionary links keys to values, like words to meanings: ages["Ann"] = 12. Looking up a key is fast, however big it is.
.get(key, 0) gives a default when the key isn't there; looping over a dictionary gives its keys, .items() gives keys and values together.
counts = {}
for word in input().split():
counts[word] = counts.get(word, 0) + 1
for word, n in counts.items():
print(word, n)Watch for: Reading a key that isn't there stops the program with a KeyError: check with in, or use .get().