728x90

(1) 문제

  • 알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.

(2) 입력

  • 첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.

(3) 출력

  • 첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.

(4) 예제 입력 및 출력


(5) 코드

#변수를 입력받는 방법
text = input()
alphabat_count = []

overlap_check = False
overlap_count_check = 0

for i in range(0, 26):
    alphabat_count.append(0)

for i in range(0, len(text)):
    # text_ascii = ord(text[i])
    # index = 0
    # #대문자
    # if text_ascii >= 65 and text_ascii <= 90:
    #     index = text_ascii - 65
    #     alphabat_count[index] += 1
    # #소문자
    # elif text_ascii >= 97 and text_ascii <= 122:
    #     index = text_ascii - 97
    #     alphabat_count[index] += 1
    
    text_ascii = ord(text[i].upper())
    index = 0
    index = text_ascii - 65
    alphabat_count[index] += 1

max_alphabat_count = max(alphabat_count)
max_alphabat_index = alphabat_count.index(max_alphabat_count)

for i in range(0,26):
    if alphabat_count[i] == alphabat_count[max_alphabat_index]:
        overlap_count_check += 1
    if overlap_count_check >= 2:
        overlap_check = True
        break

if overlap_check:
    print('?')
else:
    print(chr(65 + max_alphabat_index))

(6) 실행결과


 

반응형

+ Recent posts