Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Most Wanted Letter by Raknoche
#I'll explain this code ahead of time, for those trying to learn from the examples
#This function works in two lines. The first line cleans up the text for handling:
#I use ''.join(char.strip(' ') for char in ...) to remove white space from the original text
#I use char in sorted(text.lower()) to sort all of the text (in lower case) aphabetically
#I use if char not in set(string.punctuation) to remove all punctuation - we need to import string for this
#I use and not char.isdigit() to remove all numbers from the text
#The second line uses the max function with a key of newtext.count to return the most frequent element in the variable newtext
#For those that don't know, a key can be passed to max to define which comparison is used - for example max(list, key=len) would return the longest member of the list
#Note that the max function will return the first letter in the case of a tie, which is why we had to sort the list ahead of time
import string
def checkio(text):
newtext=''.join(char.strip(' ') for char in sorted(text.lower()) if char not in set(string.punctuation) and not char.isdigit())
return max(newtext,key=newtext.count)
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.")
April 7, 2016
Comments: