<Python 백준>단어 공부

박서연·2023년 2월 15일

CodingTest

목록 보기
16/17

백준#1157

1. 내 코드

x = input()
#대문자로 만들기
upper_x = list()
for alpha in str(x):
    if (alpha.islower() == True):
        alpha = alpha.upper()
    upper_x.append(alpha)
# 중복되는 알파벳 제거
set_x = set(upper_x)
# 많이 사용된 알파벳 탐색
max_cnt = 0
max_alpha = list()
for alpha in str(set_x):
    cnt = upper_x.count(alpha)
    if (cnt > max_cnt):
        max_alpha.clear()
        max_cnt = cnt
        max_alpha.append(alpha)
    elif (cnt == max_cnt):
        max_alpha.append(alpha)
# 가장 많이 사용된 알파벳이 여러 개 존재할 경우 예외처리
if (len(max_alpha) == 1):
    print(max_alpha[0])
else:
    print("?")

2. 풀이

words = input().upper()
unique_words = list(set(words))

cnt_list = []
for x in unique_words :
    cnt = words.count(x)
    cnt_list.append(cnt)  

if cnt_list.count(max(cnt_list)) > 1 :  
    print('?')
else :
    max_index = cnt_list.index(max(cnt_list))  
    print(unique_words[max_index])

3. 반성점

💡 max() 함수를 사용하면 편리한데 생각하지 못하여 코드가 길어진다

4. 배운점

💡 입력받을 때 upper()함수를 사용하여 한 줄로 처리할 수 있다

0개의 댓글