Skip to content

Commit 65ae4dc

Browse files
committed
solved(python): programmers
- 2024 KAKAO WINTER INTERNSHIP / n+1 카드게임
1 parent 4574c8b commit 65ae4dc

4 files changed

Lines changed: 149 additions & 0 deletions

File tree

programmers/2024_KAKAO_WINTER_INTERNSHIP/python/n_plus_1_카드게임/__init__.py

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List, Set
2+
3+
4+
def solution(coin: int, cards: List[int]) -> int:
5+
n, count = len(cards), 1
6+
init, picked, remained = set(cards[: n // 3]), set(), cards[n // 3 :][::-1]
7+
8+
while remained:
9+
picked |= {remained.pop(), remained.pop()}
10+
11+
found = False
12+
for x, y, amount in [(init, init, 0), (init, picked, 1), (picked, picked, 2)]:
13+
if amount > coin:
14+
break
15+
16+
if (pair := find_pair(n + 1, x, y)) and pair:
17+
found = True
18+
init, picked = init - pair, picked - pair
19+
count, coin = count + 1, coin - amount
20+
break
21+
22+
if not found:
23+
break
24+
25+
return count
26+
27+
28+
def find_pair(target: int, x: Set[int], y: Set[int]) -> Set[int]:
29+
for card in x:
30+
if target - card in y:
31+
return {card, target - card}
32+
33+
return set()
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
[
2+
{
3+
"params": [
4+
4,
5+
[
6+
3,
7+
6,
8+
7,
9+
2,
10+
1,
11+
10,
12+
5,
13+
9,
14+
8,
15+
12,
16+
11,
17+
4
18+
]
19+
],
20+
"expected": 5
21+
},
22+
{
23+
"params": [
24+
3,
25+
[
26+
1,
27+
2,
28+
3,
29+
4,
30+
5,
31+
8,
32+
6,
33+
7,
34+
9,
35+
10,
36+
11,
37+
12
38+
]
39+
],
40+
"expected": 2
41+
},
42+
{
43+
"params": [
44+
2,
45+
[
46+
5,
47+
8,
48+
1,
49+
2,
50+
9,
51+
4,
52+
12,
53+
11,
54+
3,
55+
10,
56+
6,
57+
7
58+
]
59+
],
60+
"expected": 4
61+
},
62+
{
63+
"params": [
64+
10,
65+
[
66+
1,
67+
2,
68+
3,
69+
4,
70+
5,
71+
6,
72+
7,
73+
8,
74+
9,
75+
10,
76+
11,
77+
12,
78+
13,
79+
14,
80+
15,
81+
16,
82+
17,
83+
18
84+
]
85+
],
86+
"expected": 1
87+
}
88+
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import os
3+
import unittest
4+
5+
from parameterized import parameterized
6+
7+
from .main import solution
8+
9+
10+
def load_sample(filename: str):
11+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
12+
13+
with open(path, "r") as file:
14+
return [(case["params"], case["expected"]) for case in json.load(file)]
15+
16+
17+
class TestCase(unittest.TestCase):
18+
@parameterized.expand(load_sample("sample.json"))
19+
def test_case(self, params: list, expected: any):
20+
# When
21+
result = solution(*params)
22+
23+
# Then
24+
self.assertEqual(expected, result)
25+
26+
27+
if __name__ == "__main__":
28+
unittest.main()

0 commit comments

Comments
 (0)