The only hint she gave us was that we needed to make 2 lists. and this is where she lost me.
That seems to imply that you cannot use collections.Counter
,
and probably not even dictionaries.
If we can assume that letters are defined as the English alphabet,
then one list is enough.
In that case, you could create a list of 26 items, all initialized to 0.
Then you could iterate over the characters of the string,
and for each letter of the English alphabet, increment the count of the n
-th item in the list, where n
is the index of the letter in the alphabet.
Create a list of 26 zeros:
counts = [0] * 26
Loop over the characters of the input string s
:
for c in s:
Check that the character is a letter:
if 'a' <= c.lower() <= 'z'
Calculate the 0-based index of the letter in the alphabet and increment the count:
index = ord(c.lower()) - ord('a')
counts[index] += 1
Once you have the counts,
you can find the index that has the maximum value (left for you as an exercise),
and get the corresponding character with chr(index + ord('a'))
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…