[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ4] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1620 - ๋‚˜๋Š”์•ผ ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ ์ด๋‹ค์†œ

keyneneยท2022๋…„ 10์›” 15์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
8/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ4] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1620
- ๋‚˜๋Š”์•ผ ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ ์ด๋‹ค์†œ

๐Ÿ“œ๋ฌธ์ œ

...(ํฌ์ผ“๋ชฌ ๊ฐ€์ง€๊ณ  ์น˜๊ณ ๋ฐ•๊ณ  ์‹ธ์šฐ๋Š” ๊ฑด)์ค‘๋žต...



๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

N,M์„ ์ž…๋ ฅ๋ฐ›๊ณ  pok_name, pok_num์„ ๋”•์…”๋„ˆ๋ฆฌ(dictionary)๋กœ ์„ ์–ธํ•˜์ž
pok_name์€ ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ, pok_num์€ ์ˆœ์„œ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ์ €์žฅํ•˜์ž
Mํฌ๊ธฐ๋งŒํผ ๋ฆฌ์ŠคํŠธ test์— ์ž…๋ ฅ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  test์˜ ๋ชจ๋“ ์š”์†Œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ
๋ฌธ์ž๋Š” ์ˆœ์„œ๋ฅผ, ์ˆœ์„œ๋Š” ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜์ž


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. N, M, pok_name, pok_num ์ •์˜ํ•˜์—ฌ pok_name๊ณผ pok_num์— ์ž…๋ ฅ๊ฐ’์„ dictionary ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜์ž
/*์ €์žฅ๋˜๋Š” ๋ฐฉ๋ฒ•*/
pok_name = {1: 'Bulbasaur', 2: 'Ivysaur', ...}  key : ์ˆœ์„œ (์ด๋ฆ„์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•จ)
pok_num =  {'Bulbasaur': 1, 'Ivysaur': 2, ...}  key : ์ด๋ฆ„ (์ˆœ์„œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•จ)
#๋”•์…”๋„ˆ๋ฆฌ๋Š” key๊ฐ’์„ ํƒ์ƒ‰ํ•˜์—ฌ value๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 2๊ฐ€์ง€ ์ผ€์ด์Šค๋กœ ์ €์žฅํ–ˆ์Œ
  1. test์— ๋ฆฌ์ŠคํŠธ๋กœ ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์•„ ์š”์†Œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๊ฒ€์ƒ‰ํ•˜๋ฉด์„œ ์ˆซ์ž์ธ์ง€ ํ™•์ธํ•˜๊ณ ,
    ๋ฌธ์ž โ†’ ์ˆซ์ž(pok_num), ์ˆซ์ž โ†’ ๋ฌธ์ž(pok_name)๋กœ ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅํ•˜์ž

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

import sys
input = sys.stdin.readline

n,m = map(int, input().split())
pok_name = {}
pok_num = {}

for i in range(1,n+1):
	#1~N๊นŒ์ง€ ์ž…๋ ฅ๊ฐ’ ๋ฐ›๊ธฐ
    name = input().rstrip()
    pok_name[i] = name  
    pok_num[name] = i   

#testcase ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ˆซ์žโ†’๋ฌธ์ž / ๋ฌธ์žโ†’์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜
test = [input().rstrip() for _ in range(m)]
for i in test:
    if i.isdigit():
        print(pok_name[int(i)])
    else:
        print(pok_num[i])

โœ๏ธ1. N, M ์ €์žฅํ•˜๊ณ  pok_name, pok_num์„ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ €์žฅ

import sys
input = sys.stdin.readline

n,m = map(int, input().split())
pok_name = {}
pok_num = {}

for i in range(1,n+1):
    name = input().rstrip()
    pok_name[i] = name
    pok_num[name] = i
    โ€ป2๊ฐ€์ง€ ์ผ€์ด์Šค๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์ €์žฅํ•˜๋Š” ์ด์œ ?
    ์ž…๋ ฅ๊ฐ’์ด ์ˆซ์ž(์ˆœ์„œ)๋ผ๋ฉด ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜, pok_name์„ ์‚ฌ์šฉ
    ##pok_name = {1: 'Bulbasaur', 2: 'Ivysaur', ...}
    ์ž…๋ ฅ๊ฐ’์ด ๋ฌธ์ž(์ด๋ฆ„)๋ผ๋ฉด ํฌ์ผ“๋ชฌ์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜ํ™˜, pok_num์„ ์‚ฌ์šฉ
    ##pok_num = {'Bulbasaur': 1, 'Ivysaur': 2, ...}

โœ๏ธ2. ๋น„๊ต๊ฐ’(test)์ด ์ˆซ์ž์ธ์ง€(.isdigit())ํ™•์ธํ•˜์—ฌ ๊ฒฐ๊ณผ๊ฐ’ ์ถœ๋ ฅ

test = [input().rstrip() for _ in range(m)]
for i in test:
    if i.isdigit(): #์ˆซ์ž๋ผ๋ฉด name์„ ๋ฐ˜ํ™˜
        print(pok_name[int(i)])
    else:  #์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด num์„ ๋ฐ˜ํ™˜
        print(pok_num[i])

๐Ÿ“š์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ค๋ฅ˜์™€ ์ •๋ฆฌ

โ€ป์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜

pok = {} ์œผ๋กœ {name:num, name:num ...}์ €์žฅ ํ›„ 
๋น„๊ต๊ฐ’์œผ๋กœ name์ด ๋“ค์–ด์˜ค๋ฉด num์œผ๋กœ, num์ด ๋“ค์–ด์˜ค๋ฉด name์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋ ค๊ณ  ํ–ˆ์œผ๋‚˜,
๋”•์…”๋„ˆ๋ฆฌ๋ฅผ 2๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ์ €์žฅํ•ด์„œ ๋น„๊ตํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•œ ๊ฒƒ์ด๋ผ๊ณ  ํŒ๋‹จํ•จ

  1. ๋น„๊ต๊ฐ’์ด n์ธ๊ฒฝ์šฐ m์„, m์ธ๊ฒฝ์šฐ n์„ ์ถœ๋ ฅํ•˜๋Š” ์ผ€์ด์Šค๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ 2๊ฐ€์ง€๋กœ ๋‚˜๋ˆ„๋ฉด ํŽธํ•จ
  2. str() == true๋ฟ ์•„๋‹ˆ๋ผ .isdigit()์ด๋ผ๋Š” ํ›Œ๋ฅญํ•œ ๋‚ด์žฅํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์Œ
    .isdigit()๊ฐ™์€ ๋‚ด์žฅํ•จ์ˆ˜์˜ ์ข…๋ฅ˜๋Š” ์•„๋ž˜ ๋งํฌ ์ฐธ๊ณ ! ์ •๋ฆฌ๊ฐ€ ์ž˜ ๋˜์–ด์žˆ์Œ
    code_angler๋‹˜ ํฌ์ŠคํŒ… (ํŒŒ์ด์ฌ(Python) ๋ฌธ์ž/์ˆซ์ž์ธ์ง€ ํ™•์ธํ•˜๊ธฐ[isalpha, isdigit, isalnum])
profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€