Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Use built-ins solution in Speedy category for The Most Wanted Letter by cort6284
import string
from collections import Counter
def checkio(text):
# lower-case-ize text, then remove any non-letter characters
text = [char for char in text.lower()
if char in string.ascii_lowercase]
# count occurrences of letters
# returns a list of tuples [('letter', occurrences), (...)]
most_common = Counter(text).most_common()
# sort by number of occurrences (in descending order, hence -x[1]), then
# sort by letter in ascending order
most_common.sort(key=lambda x: (-x[1], x[0]))
# thanks to the sorting, the desired result will be the letter in the first
# element of the list
return most_common[0][0]
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
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."
Feb. 22, 2014
Comments: