[백준 BOJ_1620] 나는야 포켓몬 마스터 이다솜 Python 풀이

출처: 백준 온라인 저지

문제

image image image image image image

풀이

dictionary를 이용하여 풀어주었습니다.

우선 N만큼의 포켓몬 입력을 list로 받아준 뒤, 포켓몬의 이름을 key로, 포켓몬의 번호를 value로 가지는 numbers를 pokemons list를 이용해 만들어주었습니다.

그 다음은 나오는 문제에 맞춰서 출력해줍니다.

코드

import sys
input = sys.stdin.readline

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

pokemons = [ input().rstrip() for _ in range(N)]
numbers = {p: num+1 for num, p in enumerate(pokemons)}
# print(pokemons, numbers)
for _ in range(M):
  quiz = input().rstrip()
  if quiz.isnumeric():
    print(pokemons[int(quiz)-1])
  else:
    print(numbers[quiz])

Leave a comment