単語の学習(1157)



String


質問する


アルファベットの大文字と小文字の単語を指定した場合は、この単語で最も使用されるアルファベットが何であるかを決定するプログラムを作成します.ただし、大文字と小文字は区別されません.

入力


最初の行には、アルファベットの大文字と小文字からなる単語が表示されます.与えられた単語の長さは10万を超えない.

しゅつりょく


最初の行は、この語で最も多く使われているアルファベットを大文字で出力します.しかし、最も使用頻度の高いアルファベットが複数存在する場合?出力します.

100%に達した後、最後にランタイムエラーが発生しました.
string = input().lower()
dic = {}

for i in range(len(string)):
    if string[i] in dic:
        dic[string[i]] += 1
    elif string[i] not in dic:
        dic[string[i]] = 1
        
dic = list(dic.items())
dic.sort(key = lambda x: x[1], reverse = True)


if dic[0][1] == dic[1][1]:
    print('?')
else:
    print(dic[0][0].upper())
ディクショナリではなく配列を使用してアルファベット周波数のコメントをチェック
word = input().lower()
wordList = list(set(word))
wordCount = []

for i in wordList: # 각 알파벳 유닛의 빈도를 wordCount에 넣는다.
    count = word.count(i)
    wordCount.append(count)
    
maxValue = max(wordCount) # 최다 빈도

if wordCount.count(maxValue) >= 2: # 최다 빈도(maxValue) 알파벳이 2번 이상 나오는지 체크
    print('?')
else:
    answer = wordCount.index(maxValue) # 최다 빈도 알파벳 추출
    print(wordList[answer].upper())
確かに、文字列の問題ではsetや配列を使う感覚が重要です.
参照)
https://ooyoung.tistory.com/70