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 jollyca
def checkio(text):
""" Find the most frequent letter in an expression
Based on the algorithms used at http://rosettacode.org/wiki/Letter_frequency
"""
# Edge case - one letter.
if len(text)==1 and ord(text) in range(ord('a'),ord('z')+1):
return text
# choose only the letters from the text
new_string = [letter for letter in text.lower() if ord(letter) in range(ord('a'),ord('z')+1)]
new_string = ''.join(new_string)
# define a list of frequencies
freqs = [0 for i in range(0,26)]
for letter in new_string:
# increase the frequency for the corresponding letter
freqs[ord(letter)-ord('a')]+=1
# choose the maximum value from the frequencies
max_value = max(freqs)
# determine the index of the maximum value
max_index = sorted([index for index, value in enumerate(freqs) if value == max_value])
# determine the letter corresponding to the index and return
return (chr(ord('a') + max_index[0]))
#These "asserts" using only for self-checking and not necessary for auto-testing
April 24, 2014
Comments: