패션 마을의 옷장 마법사
옛날 옛적, 패션 마을이라는 곳에 아리라는 이름의 옷장 마법사가 살고 있었습니다. 아리는 마을 사람들에게 멋진 옷을 조합해주는 특별한 능력을 가지고 있었죠.
어느 날, 마을에 새로운 옷들이 도착했습니다. 아리는 이 옷들을 잘 정리하고, 마을 사람들에게 다양한 스타일을 제안하기로 마음먹었습니다. 옷들은 각각 종류가 있었는데, 예를 들어 "노란 모자"는 머리장식, "파란 선글라스"는 액세서리, "초록 터번"도 머리장식에 속했죠.
아리는 먼저 각 종류별로 옷의 개수를 세기 시작했습니다. 머리장식에는 노란 모자와 초록 터번, 액세서리에는 파란 선글라스가 있었습니다. 그러고 나서, 각 종류별로 하나를 선택하거나 선택하지 않는 경우의 수를 계산했습니다. 예를 들어, 머리장식은 2가지 선택지(노란 모자, 초록 터번)와 선택하지 않는 경우까지 포함하여 총 3가지 경우가 있었고, 액세서리는 1가지 선택지와 선택하지 않는 경우로 총 2가지 경우가 있었습니다.
아리는 모든 종류의 경우를 곱한 후, 아무것도 선택하지 않는 경우를 제외했습니다. 이를 통해 마을 사람들이 만들 수 있는 다양한 옷 조합의 수를 알아냈죠. 최종적으로, 아리는 마을 사람들에게 총 5가지의 멋진 옷 조합을 제안할 수 있게 되었습니다.
마을 사람들은 아리의 도움으로 매일매일 다른 스타일을 즐기며 행복하게 살았답니다.
- G
ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ
Hash map
def solution(clothes):
# 1. Create a hash map to count each type of clothing
hash_map = {}
for clothes, type in clothes:
hash_map[type] = hash_map.get(type, 0) + 1
# get(type, 0) : If the type doesn't exist, start count at 0
# 2. Multiply the number of combinations for each clothing type
answer = 1
for type in hash_map:
answer *= (hash_map[type] + 1)
# 3. Subtract 1 to exclude the case where nothing is worn
return answer - 1
print(solution([["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]]))
5
Counter와 reduce
from collections import Counter
from functools import reduce
def solution(clothes):
# 1. Count each type of clothing
counter = Counter([type for clothes, type in clothes])
# 2. Multiply the combinations (including not wearing an item)
answer = reduce(lambda x, y: x * (y + 1), counter.values(), 1)
# 3. Subtract 1 to exclude the case of wearing nothing
return answer - 1
print(solution([["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]]))
5