728x90

나의 풀이
from collections import Counter
def solution(clothes):
gear = list()
for n in clothes:
gear.insert(0, n[1])
gear = Counter(gear).values()
result = 1
for n in gear:
result *= (n + 1)
return result - 1
Reduce를 활용한 풀이 1
def solution(clothes):
from collections import Counter
from functools import reduce
cnt = Counter([kind for name, kind in clothes])
answer = reduce(lambda x, y: x*(y+1), cnt.values(), 1) - 1
return answer
Reduce를 활용한 풀이 2
import collections
from functools import reduce
def solution(c):
return reduce(lambda x,y:x*y,[a+1 for a in collections.Counter([x[1] for x in c]).values()])-1
해시를 활용한 풀이
def solution(clothes):
clothes_type = {}
for c, t in clothes:
if t not in clothes_type:
clothes_type[t] = 2
else:
clothes_type[t] += 1
cnt = 1
for num in clothes_type.values():
cnt *= num
return cnt - 1반응형
'Coding Test > Programmers' 카테고리의 다른 글
| [프로그래머스/파이썬] 기능개발 (0) | 2022.05.31 |
|---|---|
| [프로그래머스/파이썬] 베스트앨범 (0) | 2022.05.24 |
| [프로그래머스/파이썬] 전화번호 목록 (0) | 2022.05.24 |
| [프로그래머스/파이썬] 완주하지 못한 선수 (0) | 2022.05.24 |
| [프로그래머스/MySQL] 상위 n개 레코드 (0) | 2022.05.23 |