[๋ฌธ์ ๋งํฌ] ๐
SW Expert Academy
SW ํ๋ก๊ทธ๋๋ฐ ์ญ๋ ๊ฐํ์ ๋์์ด ๋๋ ๋ค์ํ ํ์ต ์ปจํ ์ธ ๋ฅผ ํ์ธํ์ธ์!
swexpertacademy.com
ํ์ด
๋ช ๋ น์ด ๋ถ๋ฆฌ ๋ฐ ์ฒ๋ฆฌ
- ๋ช ๋ น์ด๋ ์ฝ์ (I)๊ณผ ์ญ์ (D) ๋ ๊ฐ์ง๊ฐ ์ฃผ์ด์ง๋ฏ๋ก, ์ด๋ฅผ ๊ตฌ๋ถํด ์์ ์ ์ํํ๋ค.
- ๋ช ๋ น์ด๋ฅผ split()์ผ๋ก ๋ถ๋ฆฌํ๊ฑฐ๋ ํน์ ๊ตฌ๋ถ์๋ก ๋๋ , I ๋๋ D๋ฅผ ๊ตฌ๋ถํ๋ค.
- replace() ํ split() ์ฌ์ฉํ๊ธฐ
์ฝ์ ๋ฐ ์ญ์ ์ฒ๋ฆฌ
- ๊ตฌ๋ถํ ๋ฆฌ์คํธ๋ฅผ for ๋ฌธ์ผ๋ก ํ๋์ฉ ๊บผ๋ด์ด ๋ช ๋ น์ด๋ฅผ ๋ฐ๋ก ์ ์ฅํ๋ค.
- ๊ฐ๋ณ ๋ช ๋ น์ด๋ ๋งจ ์์ I ํน์ D ๊ฐ ์์ด์ ๋ช ๋ น์ด์ ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค๋ฅผ ๊ธฐ์ค์ผ๋ก ์กฐ๊ฑด์ ๋ถ๋ฆฌํ๋ค.
- ์ฌ๋ผ์ด์ฑ์ ์ด์ฉํ์ฌ ์ฝ์ ๊ณผ ์ญ์ ์ฐ์ฐ์ ํ๋ค.
์ถ๋ ฅ
- ์ํธ๋ฌธ์ ์ 10๊ฐ ์ซ์๋ฅผ ์ถ๋ ฅํ๋ค.
Solution
for test_case in range(1, 11):
_ = int(input())
origin = list(map(int, input().split()))
_ = int(input())
commands = input().replace("I", ";I").replace("D", ";D") # I์ D๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถ
# ";"๋ก ๋๋ ๋ช
๋ น์ด ๋ถ๋ฆฌ
commands = commands.split(";")[1:] # ์ฒซ ๋ถ๋ถ์ ๋น ๋ฌธ์์ด์ด๋ฏ๋ก ์ ์ธ
for i in commands:
command = list(map(str, i.split())) # ๋ช
๋ น์ด ๋ฐ๋ก ์ ์ฅ
if command[0] == "I": # insert ์ฐ์ฐ
int_command = list(map(int, command[1:]))
insert_position = int_command[0] # ์ฝ์
ํ ์์น
insert_command = int_command[2:] # ๋ช
๋ น์ด ์ ์ฅ
origin[insert_position:insert_position] = insert_command
else: #delete ์ฐ์ฐ
int_command = list(map(int, command[1:]))
del origin[int_command[0]:int_command[0] + int_command[1]]
print(f"#{test_case} {' '.join(map(str, origin[:10]))}")
๊ฐ์ ํ ๋ถ๋ถ
- ๋ถํ์ํ ๋ณํ ์ค์ด๊ธฐ
- command = list(map(str, i.split())) ๋์ i.split()๋ง์ผ๋ก ์ถฉ๋ถํ๋ค. ๋ํ int_command๋ก ๋ณํํ ๋๋ ํ์ํ ๊ฒฝ์ฐ์๋ง ์ซ์๋ก ๋ณํํ๋ฉด ๋๋ค.
- ๋ฆฌ์คํธ ์ฌ๋ผ์ด์ฑ์ ํ์ฉํ ์ฝ์
- origin[insert_position:insert_position] = insert_command๋ ํจ์จ์ ์ด์ง๋ง, ๋๋ ์ฝ์ ์ ํ ๋ origin.insert()๋ฅผ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ๋ ๋ฐฉ์๋ ๊ฐ๋ฅํ๋ค.
- del๋ก ์ญ์ ์ต์ ํ
- ์ญ์ ๋ถ๋ถ์ origin.pop()์ ์ฌ์ฉํ์ฌ ๊ฐ ์์๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ์ญ์ ํ๋ ๊ฒ์ด ๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ ์ ์์ง๋ง, ์ฌ๊ธฐ์๋ ์ฌ๋ผ์ด์ฑ์ด ํจ์จ์ ์ผ ์ ์๋ค.
์ต์ ํ๋ ์ฝ๋
for test_case in range(1, 11):
_ = int(input()) # ์๋ณธ ์ํธ๋ฌธ ๊ธธ์ด
origin = list(map(int, input().split())) # ์๋ณธ ์ํธ๋ฌธ
_ = int(input()) # ๋ช
๋ น์ด ๊ฐ์
commands = input().replace("I", ";I").replace("D", ";D") # I์ D๋ก ๊ตฌ๋ถํ์ฌ ๋ช
๋ น์ด ๋ถ๋ฆฌ
commands = commands.split(";")[1:] # ๋น ์ฒซ ๋ถ๋ถ ์ ์ธ
for i in commands:
command = i.split() # ๋ช
๋ น์ด ๋ถ๋ฆฌ
cmd_type, x, y = command[0], int(command[1]), int(command[2])
if cmd_type == "I": # insert ์ฐ์ฐ
s = map(int, command[3:]) # ์ฝ์
ํ ์ซ์๋ค
origin[x:x] = s # ๋ฆฌ์คํธ ์ฌ๋ผ์ด์ฑ์ผ๋ก ํ๋ฒ์ ์ฝ์
elif cmd_type == "D": # delete ์ฐ์ฐ
del origin[x:x + y] # ๋ฆฌ์คํธ ์ฌ๋ผ์ด์ฑ์ผ๋ก ์ญ์
print(f"#{test_case} {' '.join(map(str, origin[:10]))}")
๐ฉ๐ป ํ๊ณ
import re ์ฌ์ฉํ ํ์ด
import re
for test_case in range(1, 11):
_ = int(input())
origin = list(map(int, input().split()))
_ = int(input())
commands = input()
commands = re.split('[I|D]', commands) # ์ฌ๋ฌ ๊ตฌ๋ถ์ ์ฌ์ฉ, I์ D๋ก ๊ตฌ๋ถํ๋ค.
for i in commands[1:]: # ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค๋ ๊ณต๋ฐฑ์ด๊ธฐ ๋๋ฌธ์ ํจ์ค
command = list(map(int, i.split())) # ๋ช
๋ น์ด ๋ฐ๋ก ์ ์ฅ
# insert ์ delete ์ธ๊ฑธ ๊ตฌ๋ถํ๋ ค๋ฉด ๊ธธ์ด๋ก ํ๋จ
if len(command) >= 3: # ๊ธธ์ด๊ฐ 2๋ณด๋ค ํฌ๋ฉด insert ์ฐ์ฐ
insert_position = command[0] # ์ฝ์
ํ ์์น
insert_command = command[2:] # ๋ช
๋ น์ด ์ ์ฅ
origin[insert_position:insert_position] = insert_command
else: # 2 ์ดํ์ด๋ฉด delete ์ฐ์ฐ
del origin[command[0]:command[0] + command[1]]
print(f"#{test_case} {' '.join(map(str, origin[:10]))}")
swea ์์๋ import re ๊ฐ ์ ๋๋ค๋ ๊ฑธ ์๊ณ ํ๋ค๊ฐ ๋ญํจ ๋ดค๋ค. ์ด์ ์ ์ญ์ ์ฒ๋ฆฌ๊ฐ ์๋ ์ํธ๋ฌธ ๋ฌธ์ ๋ฅผ ํ๊ณ ์์ ์ฝ์ ์ญ์ ์ฒ๋ฆฌํ๋ ๋ถ๋ถ์ ์ด๋ ต์ง ์๊ฒ ํ์๋ค. ๊ทธ๋ฌ๋ ์ฌ๋ฌ ๊ตฌ๋ถ์๋ก ๋ถ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ชฐ๋ผ์ ๋์ด๋ ๊ธ์์น.. ์ฌ์ง์ด re.split() ๋ ์ ๋ผ์ ๋ง๋งํ์๋ค. ์๋ง import re ๋ฅผ ํ์ฉํด์คฌ์ผ๋ฉด ๋ง์ง ์์์๊น ์ถ๋ค.
๊ธฐ์ตํด์ผ ํ๋ ์
๋ฌธ์์ด์ ๊ตณ์ด map๊ณผ list๋ก ๊ฐ์ ํ์๊ฐ ์๋ค.! split() ์ ์ฌ์ฉํ๊ณ ์ถ์ ๋๋ ์ฌ์ฉํ ํ์๊ฐ ์์ด ์์์ ๋ฆฌ์คํธํ๋ฅผ ํด์ค๋ค.
'๐งฉ Algorithm > SWEA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [SWEA] 1222. [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 6์ผ์ฐจ - ๊ณ์ฐ๊ธฐ1 (Python/D4) (0) | 2024.10.31 |
|---|---|
| [SWEA] 13547. ํ์จ๋ฆ (Python/D3) (0) | 2024.10.30 |
| [SWEA] 1228. [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 8์ผ์ฐจ - ์ํธ๋ฌธ1 (Python/D3) (0) | 2024.10.30 |
| [SWEA] 5356. ์์์ด์ ์ธ๋ก๋ก ๋งํด์ (Python/D3) (1) | 2024.10.29 |
| [SWEA] 11736. ํ๋ฒํ ์ซ์ (Python/D3) (0) | 2024.10.29 |