[Boj] 나는야 포켓몬 이다솜 1620(python)
문제링크 : https://www.acmicpc.net/problem/1620
포켓몬 N개가 주어지고, 다음 M줄은 숫자 또는 알파벳이 입력되어 이에 해당하는 포켓몬 혹은 인덱스를 출력하는 문제
간단하게 인덱스를 키로 포켓몬을 값으로 하는 딕셔너리와 포켓몬을 키로 인덱스를 값으로 하는 딕셔너리로 관리
dict.get(x)
는 x에 해당하는 키가 없으면 None을 반환하고, 있다면 해당 값을 반환함
이를 이용하면 해결
import sys
N, M = map(int, input().split())
cnt = 1
dict_alpa = {}
dict_num = {}
for _ in range(N):
poke = sys.stdin.readline().rstrip()
dict_alpa[poke] = cnt
dict_num[str(cnt)] = poke
cnt += 1
for _ in range(M):
question = sys.stdin.readline().rstrip()
if dict_alpa.get(question) is not None:
print(dict_alpa.get(question))
if dict_num.get(question) is not None:
print(dict_num.get(question))
댓글남기기