티스토리 뷰
📚 문제




입력

출력

예제 입력
26 5
Bulbasaur
Ivysaur
Venusaur
Charmander
Charmeleon
Charizard
Squirtle
Wartortle
Blastoise
Caterpie
Metapod
Butterfree
Weedle
Kakuna
Beedrill
Pidgey
Pidgeotto
Pidgeot
Rattata
Raticate
Spearow
Fearow
Ekans
Arbok
Pikachu
Raichu
25
Raichu
3
Pidgey
Kakuna
예제 출력
Pikachu
26
Venusaur
16
14
🧑🏻💻 풀이 과정
- 파이썬의 딕셔너리를 사용하자.
- 숫자를 입력 받았을 때, 바로 찾기 위해서 1부터 n+1만큼 반복하자.
- 나중에 입력받을 이름과 번호를 각각 구분해서 value로 출력하기 위해
pokemon
에는 이름이 key,number_pokemon
에는 숫자가 key로 초기화해주자. - m만큼 반복하면서 입력이 숫자로 들어오면
number_pokemon
의 value를 반환하고, 문자로 들어오면pokemon
의 value를 반환하자.
import sys
def pokemon_master(n, m):
pokemon = {}
number_pokemon = {}
for i in range(1, n + 1):
name = sys.stdin.readline().rstrip()
pokemon[name] = pokemon.setdefault(name, i)
number_pokemon[i] = pokemon.setdefault(i, name)
for i in range(m):
name = sys.stdin.readline().rstrip()
if name.isnumeric():
print(number_pokemon[int(name)])
else:
print(pokemon[name])
n, m = map(int, sys.stdin.readline().rstrip().split())
pokemon_master(n, m)
'알고리즘' 카테고리의 다른 글
[algorithm] 백준 25192 - 인사성 밝은 곰곰이 (파이썬) (0) | 2022.09.03 |
---|---|
[algorithm] 백준 11899 - 괄호 끼어넣기 (파이썬) (0) | 2022.09.02 |
[algorithm] 백준 10866 - 덱 (파이썬) (0) | 2022.08.31 |
[algorithm] 백준 17219 - 비밀번호 찾기 (파이썬) (0) | 2022.08.30 |
[algorithm] 백준 10845 - 큐 (파이썬) (0) | 2022.08.29 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 코테
- 리팩토링
- webflux
- spring boot
- 노마드코더
- 정렬
- 스프링부트
- mysql 8.0
- kotlin
- 데이터베이스
- Algorithm
- 자료구조
- 노마드
- Spring
- MySQL
- 구현
- 코틀린
- 북클럽
- 그리디
- leetcode
- 릿코드
- 알고리즘
- 문자열
- 스프링
- 백준
- 파이썬
- 인프런
- 스프링 부트
- 김영한
- Real MySQL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함