Coding Test/programmers

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

파송송 2022. 11. 24. 15:53
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/42578

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


나의 풀이

"headgear" - "yellow_hat","green_turban"

"eyewear" - "blue_sunglasses"

이렇게 있다고 할 때, 0 * 2 + 2 = 2, 2 * 1 + 1 이런 식으로 위의 조합에서 경우의 수를 곱해줌 

dict에 종류를 key로 하여 item을 넣어주고 len을 이용하여 계산함

def solution(clothes):
    clothes_dict = dict()
    ans = 0

    for i in clothes:
        if i[1] not in clothes_dict.keys():
            clothes_dict[i[1]] = list()
            clothes_dict[i[1]].append(i[0])
        else:
            clothes_dict[i[1]].append(i[0])

    for i in clothes_dict:
        cnt_clothes = len(clothes_dict[i])
        ans += ans * cnt_clothes + cnt_clothes

    return ans


다른 사람의 풀이

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

 

728x90