Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear Explanation for Counting Letters in Text solution in Clear category for The Most Wanted Letter by mithilkotak
def checkio(text: str) -> str:
# your code here
counts={} #Dictionary to store letters and the no. of times they appear in text
for i in text: #For each character in text,
if i.isalpha(): #if the character is an alphabetic letter then
try: #try
counts[i.lower()]+=1 #adding 1 to the no. of times the letter appeared
#
except KeyError: #but if KeyError is raised (because the letter is seen for the first time)
counts|={i.lower():1} #then add the letter to the dictionary with a count of 1
#Create a list of those letters in sorted order which appear maximal times, and return the first letter
return [i for i in sorted(counts.keys()) if counts[i]==max(counts.values())][0]
print("Example:")
print(checkio("Hello World!"))
assert checkio("Hello World!") == "l"
assert checkio("How do you do?") == "o"
assert checkio("One") == "e"
assert checkio("Oops!") == "o"
assert checkio("AAaooo!!!!") == "a"
assert checkio("abe") == "a"
print("The mission is done! Click 'Check Solution' to earn rewards!")
Sept. 6, 2022