문제/백준_파이썬
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에 접근하도록 해주었다.