본문 바로가기
💻 하나씩 차곡차곡/프로그래머스 (Python)

[프로그래머스/Lv.1/Python] 의상

by 뚜루리 2024. 1. 9.
728x90
320x100
def solution(clothes):
    answer = 0
    cntList = []
    clotheslist = {}
    for n, t in clothes:
        if t in clotheslist:
            clotheslist[t].append(n)
        else:
            clotheslist[t] = [n]

    for cloth in clotheslist:
        cntList.append(len(clotheslist[cloth]))

    print(clotheslist, cntList)

    # 옷종류가 하나일 때
    if len(cntList) == 1:
        return cntList[0]
    else :
        cnt = 1
        for i in cntList:
            cnt *= i

        return sum(cntList) + cnt

    return answer

처음에 이렇게 구현했는데 테스트는 통과하고 제출은 통과못함. 소스도 너무 길고...문제가 있다.

 

 

 

def solution2(clothes):
    dict = {}
    # 옷종류 : 갯수 ex) {'headgear': 2}
    for clothe, type in clothes:
        dict[type] = dict.get(type, 0) + 1

    answer = 1
    for type in dict:

        answer *= (dict[type] + 1)
        print(answer, dict[type] + 1)

    return answer - 1 #아무 것도 안입는 경우는 없으니 -1

 

 

 

 

 

728x90
320x100