-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05_shoe_pairing.py
65 lines (50 loc) · 1.29 KB
/
05_shoe_pairing.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Challenge #5: 👢 Shoe Pairing."""
from collections import defaultdict
from typing import TypedDict
class Boot(TypedDict):
"""Boot dict"""
type: str
size: int
def organize_shoes(shoes: list[Boot]) -> list[int]:
"""
Organize shoes.
Args:
shoes (list[Boot]): Shoes
Returns:
list[int]: Available shoes
"""
boots_count = defaultdict(lambda: {"I": 0, "R": 0})
for boot in shoes:
boots_count[boot["size"]][boot["type"]] += 1
result = []
for size, counts in boots_count.items():
pairs = min(counts["I"], counts["R"])
result.extend([size] * pairs)
return result
shoes1: list[Boot] = [
{"type": "I", "size": 38},
{"type": "R", "size": 38},
{"type": "R", "size": 42},
{"type": "I", "size": 41},
{"type": "I", "size": 42},
]
print(organize_shoes(shoes1))
# [38, 42]
shoes2: list[Boot] = [
{"type": "I", "size": 38},
{"type": "R", "size": 38},
{"type": "I", "size": 38},
{"type": "I", "size": 38},
{"type": "R", "size": 38},
]
print(organize_shoes(shoes2))
# [38, 38]
shoes3: list[Boot] = [
{"type": "I", "size": 38},
{"type": "R", "size": 36},
{"type": "R", "size": 42},
{"type": "I", "size": 41},
{"type": "I", "size": 43},
]
print(organize_shoes(shoes3))
# []