Intermediate · Dictionaries · 136 of 264
Anagrams
Two words are anagrams when they use exactly the same letters, like listen and silent. Ignore capital letters.
Input: two words, one per line. Output: yes or no
Examples
Input
listen silent
Output
yes
Input
Dusty study
Output
yes
Input
apple papel
Output
yes
2 more tests check 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().