본문 바로가기

Coding Test/Programmers

[프로그래머스/파이썬] 위장

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
반응형