Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using defaultdict solution in Clear category for The Most Wanted Letter by tomkun
from string import ascii_lowercase
from collections import defaultdict
from operator import itemgetter
def checkio(text):
freq = defaultdict(int)
text_set = set(ascii_lowercase)
for letter in text.lower():
if letter in text_set:
freq[letter]+=1
top = sorted(freq.items(), key=itemgetter(1), reverse=True)
best = top[0][1]
# not sure if the inner list comprehension actually speeds things up here
return min([x for x in top if x[1]==best] , key=itemgetter(0))[0]
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio("Hello World!") == "l", "Hello test"
assert checkio("How do you do?") == "o", "O is most wanted"
assert checkio("One") == "e", "All letter only once."
assert checkio("Oops!") == "o", "Don't forget about lower case."
assert checkio("AAaooo!!!!") == "a", "Only letters."
assert checkio("abe") == "a", "The First."
print("Start the long test")
assert checkio("a" * 9000 + "b" * 1000) == "a", "Long."
print("The local tests are done.")
Feb. 21, 2015
Comments: