본문 바로가기
백준/Python

[Python] 백준 1157번: 단어 공부

by ewaterland 2023. 5. 17.

코드

input_str = input()
count = [0] * 26 # The number of alphabet
alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

# Counting the alphabets
for i in range(len(input_str)):
  if input_str[i].upper() == 'A':
    count[0] += 1
  elif input_str[i].upper() == 'B':
    count[1] += 1
  elif input_str[i].upper() == 'C':
    count[2] += 1
  elif input_str[i].upper() == 'D':
    count[3] += 1
  elif input_str[i].upper() == 'E':
    count[4] += 1
  elif input_str[i].upper() == 'F':
    count[5] += 1
  elif input_str[i].upper() == 'G':
    count[6] += 1
  elif input_str[i].upper() == 'H':
    count[7] += 1
  elif input_str[i].upper() == 'I':
    count[8] += 1
  elif input_str[i].upper() == 'J':
    count[9] += 1
  elif input_str[i].upper() == 'K':
    count[10] += 1
  elif input_str[i].upper() == 'L':
    count[11] += 1
  elif input_str[i].upper() == 'M':
    count[12] += 1
  elif input_str[i].upper() == 'N':
    count[13] += 1
  elif input_str[i].upper() == 'O':
    count[14] += 1
  elif input_str[i].upper() == 'P':
    count[15] += 1
  elif input_str[i].upper() == 'Q':
    count[16] += 1
  elif input_str[i].upper() == 'R':
    count[17] += 1
  elif input_str[i].upper() == 'S':
    count[18] += 1
  elif input_str[i].upper() == 'T':
    count[19] += 1
  elif input_str[i].upper() == 'U':
    count[20] += 1
  elif input_str[i].upper() == 'V':
    count[21] += 1
  elif input_str[i].upper() == 'W':
    count[22] += 1
  elif input_str[i].upper() == 'X':
    count[23] += 1
  elif input_str[i].upper() == 'Y':
    count[24] += 1
  elif input_str[i].upper() == 'Z':
    count[25] += 1

temp = 0
max_ = 0
for i in range(len(count)):
  if count[i] == max(count):
    temp += 1
    max_ = i

if temp >= 2:
  print("?")
else:
  print(alphabet[max_])

풀이

더 쉽게 구현할 수 있는 코드가 분명 있겠지만,,

어떤 알파벳이 가장 많은지 세어줘야 하고, 많은 게 여러 개 있는 것도 판단해야 하고, 그 결과에 따라 출력값이 달라져야 하는데, 조건문 달아주는 것 밖에 생각 안 나서 그냥 조건문을 많이 쓰는 걸로 구현했다.