sm 기술 블로그

92. 1620(나는야 포켓몬 마스터 이다솜) 본문

문제/백준_파이썬

92. 1620(나는야 포켓몬 마스터 이다솜)

sm_hope 2022. 6. 19. 15:26
import sys
input = sys.stdin.readline

N, M = map(int, input().split())

Pokemon = list(input().strip() for _ in range(N))
PokemonQuestion = list(input().strip() for _ in range(M))

PokemonDoGam_str = {Pokemon[i]: str(i+1) for i in range(len(Pokemon))}
PokemonDoGam_num = dict(map(reversed, PokemonDoGam_str.items()))

for val in PokemonQuestion:
    if(val.isalpha()):  # 만약 문자열이 알파벳이라면
        print(PokemonDoGam_str[val])
    else:
        print(PokemonDoGam_num[val])

문제요약

다솜이는 포켓몬마스터가 되기 위해 오박사가 내는 문제를 맞춰야한다.

숫자가 들어오면 포켓몬 이름을, 포켓몬 이름이 들어오면 숫자를 말해라.

설명

문제 설명보다 잡소리가 긴 문제로 이런 병맛을 좋아하는 나에게는 취향저격이다.

빨간 선을 기준으로 위에는 포켓몬 도감 아래는 문제이다.

PokemonDoGam_str = {Pokemon[i]: str(i+1) for i in range(len(Pokemon))}
PokemonDoGam_num = dict(map(reversed, PokemonDoGam_str.items()))

데이터 사전을 번호가 key일 경우, 이름이 key일 경우 두가지를 만들어 주었다.

비교가 쉽게 번호도 문자열로 저장하였다.

 

PokemonDoGam_num = dict(map(reversed, PokemonDoGam_str.items()))

dict은 사전이고 reversed는 key와 value를 바꿔 준다. 

PokemonDoGam_str.items()를 붙여줌으로써 값들을 반전시킬 수 있다.

 

for val in PokemonQuestion:
    if(val.isalpha()):  # 만약 문자열이 알파벳이라면
        print(PokemonDoGam_str[val])
    else:
        print(PokemonDoGam_num[val])

isalpha(), isdigit(), isalnum() 등 문자열이 어떤 구성인지 확인하는 내장함수가 있다.

순서대로 문자열이 문자일 경우, 숫자일경우, 문자혹은숫자일 경우 이다.

반환은 True, False로 하고, 문제를 낸 받았을 때, 숫자인지 문자인지 확인하여 알맞는 dict에 접근하도록 해주었다.

 

 

'문제 > 백준_파이썬' 카테고리의 다른 글

94. 1764(듣보잡)  (0) 2022.06.20
93. 10816(숫자 카드2)  (0) 2022.06.19
91. 14425 (문자열 집합)  (0) 2022.06.19
90. 10815(숫자카드)  (0) 2022.06.19
89. 18870 (좌표압축)  (0) 2022.06.18
Comments