[๋ฌธ์ ๋งํฌ] ๐
SW Expert Academy
SW ํ๋ก๊ทธ๋๋ฐ ์ญ๋ ๊ฐํ์ ๋์์ด ๋๋ ๋ค์ํ ํ์ต ์ปจํ ์ธ ๋ฅผ ํ์ธํ์ธ์!
swexpertacademy.com
ํ์ด ๋ฐฉ๋ฒ
์นด๋ ์ ๋ ฅ ๋ถํ ๋ฐ ์ด๊ธฐํ
S, D, H, C = [0]*13, [0]*13, [0]*13, [0]*13
- S, D, H, C์ ๋ค ๊ฐ์ง ๋ฌด๋ฌ์ ๋ํด ๊ฐ๊ฐ 13์นธ์ง๋ฆฌ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ ๋ค.
- ๊ฐ ๋ฆฌ์คํธ๋ 13๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ๊ฐ์ง๋ฉฐ, ๊ฐ ์ธ๋ฑ์ค๋ ์นด๋ ๋ฒํธ(1~13)์ ๋์๋๋๋ก ํ๋ค.
์นด๋ ์ฝ๊ณ ์นด์ดํ
for i in range(0, len(card), 3):
shape = card[i] # ์นด๋ ์ข
๋ฅ
num = int(card[i + 1:i + 3]) # ์นด๋ ์
...
- ์นด๋ ์ ๋ ฅ ๋ฌธ์์ด์ 3๊ธ์์ฉ ์ฌ๋ผ์ด์ฑํด ์นด๋์ ๋ฌด๋ฌ์ ๋ฒํธ๋ฅผ ์ถ์ถํ๋ค.
- ๋ฌด๋ฌ์ ๋ฐ๋ผ ํด๋น ๋ฆฌ์คํธ์ ์นด๋ ๋ฒํธ ์์น์ 1์ ์ถ๊ฐํ๋ค.
์ค๋ณต ํ์ธ
- ์นด๋ ๋ฆฌ์คํธ๋ฅผ ํ์ธํด 2 ์ด์์ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ, ํด๋น ์นด๋๊ฐ ์ค๋ณต์์ ์๋ฏธํ๋ฏ๋ก ERROR๋ฅผ ์ถ๋ ฅํ๋๋ก ์ค์ ํ๋ค.
๋จ์ ์นด๋ ๊ฐ์ ๊ณ์ฐ
result = [S.count(0), D.count(0), H.count(0), C.count(0)]
- ์ค๋ณต์ด ์๋ค๋ฉด 0 ๊ฐ์ด ๋จ์ ์นด๋ ์์ด๋ฏ๋ก, ๊ฐ ๋ฌด๋ฌ๋ณ๋ก ๋จ์ ์นด๋ ์๋ฅผ ๊ณ์ฐํ๊ณ ์ถ๋ ฅํ๋ค.
Solution
T = int(input()) # ํ
์คํธ ์ผ์ด์ค ์
for test_case in range(1, T + 1):
card = input().strip()
S, D, H, C = [0]*13, [0]*13, [0]*13, [0]*13
for i in range(0, len(card), 3):
shape = card[i] # ์นด๋ ์ข
๋ฅ
num = int(card[i + 1:i + 3]) # ์นด๋ ์
if shape == 'S':
S[num - 1] += 1
elif shape == 'D':
D[num - 1] += 1
elif shape == 'H':
H[num - 1] += 1
elif shape == 'C':
C[num - 1] += 1
result = [S, D, H, C]
error = ""
for i in result:
for j in i:
if j > 1:
error = "ERROR"
break
result = [S.count(0), D.count(0), H.count(0), C.count(0)]
if error == "":
print(f"#{test_case}", *result)
else:
print(f"#{test_case} {error}")
๊ฐ์ ํฌ์ธํธ
- ์ค๋ณต ์นด๋ ๊ฒ์ถ ๋ฐฉ์
- if j > 1๋ก ํ ๋ฒ์ด๋ผ๋ ์ค๋ณต์ด ๋ฐ๊ฒฌ๋๋ฉด ๋ฐ๋ก "ERROR"๋ฅผ ์ถ๋ ฅํ ์ ์๋๋ก ๋ฃจํ๋ฅผ ํ์ถํ๋ฉด ๋ถํ์ํ ๊ณ์ฐ์ ์ค์ผ ์ ์๋ค.
- ์นด๋ ์ค๋ณต ํ์ธ ๋ฐฉ์ ๋ณ๊ฒฝ
- ๊ฐ์๋ฅผ ์ง์ ์ธ๋ ๋์ , ์งํฉ์ ์ฌ์ฉํด ์ค๋ณต์ ์ฝ๊ฒ ํ์ธํ ์ ์๋ค. ์ค๋ณต์ด ์์ผ๋ฉด ์งํฉ์ ์ถ๊ฐํ๊ณ , ์ค๋ณต์ด ๋ฐ์ํ๋ฉด ์ฆ์ "ERROR"๋ฅผ ์ถ๋ ฅํ๋ ๋ฐฉ์์ด๋ค.
- ์นด๋ ๊ฐ์ ์นด์ดํธ ๋ฐฉ์ ๋จ์ํ
- ๊ฐ ์นด๋์ ๋ํด ์นด์ดํธ๋ง ์ฆ๊ฐ์ํค๋ฉด ๋๋ฏ๋ก ๋ฐฐ์ด ๋์ set๊ณผ dict๋ฅผ ์ฌ์ฉํด ์ฒ๋ฆฌํ ์ ์๋ค.
๊ฐ์ ๋ ์ฝ๋
T = int(input()) # ํ
์คํธ ์ผ์ด์ค ์
for test_case in range(1, T + 1):
card = input().strip()
card_set = set()
card_count = {'S': 13, 'D': 13, 'H': 13, 'C': 13}
error = False
for i in range(0, len(card), 3):
shape = card[i]
num = int(card[i + 1:i + 3])
card_key = (shape, num)
if card_key in card_set:
error = True
break
card_set.add(card_key)
card_count[shape] -= 1
if error:
print(f"#{test_case} ERROR")
else:
print(f"#{test_case}", card_count['S'], card_count['D'], card_count['H'], card_count['C'])
'๐งฉ Algorithm > SWEA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [SWEA] 9280. ์ง์ฉ์ด๋ค ์ฃผ์ฐจํ์ (Python/D3) (0) | 2024.11.14 |
|---|---|
| [SWEA] 16800. ๊ตฌ๊ตฌ๋จ ๊ฑท๊ธฐ (Python/D3) (0) | 2024.11.14 |
| [SWEA] 3260. ๋ ์์ ๋ง์ (Python/D3) (1) | 2024.11.14 |
| [SWEA] 1873. ์ํธ์ ๋ฐฐํํ๋ (Python/D3) (2) | 2024.11.13 |
| [SWEA] 10580. ์ ๋ด๋ (Python/D3) (1) | 2024.11.13 |