Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for The Most Wanted Letter by sandor.szodoray
def check_if_text_length_is_in_range(text):
return 0 < len(text) <= 10 ** 5
def initialize_dictionary(sentence):
lower_sentence = sentence.lower()
dict = {}
for c in lower_sentence:
if c.isalpha():
if c in dict.keys():
dict[c] = dict[c] + 1
else:
dict[c] = 1
return dict
def get_lowest_ascii_code(character_array):
min = ord(character_array[0])
for i in range(1,len(character_array)):
if ord(character_array[i]) < min:
min = ord(character_array[i])
return chr(min)
def checkio(text):
dict = initialize_dictionary(text)
if len(dict.keys()) < 1:
return
most_frequent_chars = []
max = 0
for key in dict.keys():
if dict[key] > max:
max = dict[key]
most_frequent_chars = [key]
elif dict[key] == max:
most_frequent_chars.append(key)
if len(most_frequent_chars) > 1:
return get_lowest_ascii_code(most_frequent_chars)
return most_frequent_chars[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.")
Oct. 2, 2015