There are several problems in your code:
- you calculate the count of the most common letter, but not the letter itself
- you are
return
inside the loop and thus after the very first letter - you never use
x
, and slicing is letter
not needed
, :
, :
def most_repeated_letters(word_1):
most_common_count = 0
most_common_letter = None
for letter in word_1:
count = word_1.count(letter)
if count > most_common_count:
most_common_count = count
most_common_letter = letter
return most_common_letter
Python, . , , max
, word_1.count
key
.
def most_repeated_letters(word_1):
return max(word_1, key=word_1.count)
, , count
, O (n²). dict
O (n).
def most_repeated_letters(word_1):
counts = {}
for letter in word_1:
if letter not in counts:
counts[letter] = 1
else:
counts[letter] += 1
return max(counts, key=counts.get)
, collections.Counter
, .