Notice
Recent Posts
Link
- Today
- Total
hye-log
[프로그래머스]위장(Python) 본문
0. 문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/42578
1. 문제 설명
1) 서로 다른 옷의 조합의 수 구하기
2. 입출력
# input
clothes = [["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]]
# output
return = 5
3. 코드
def solution(clothes):
closet = {}
answer = 1
for cloth in clothes:
if cloth[1] in closet.keys(): # 종류가 key에 있으면
closet[cloth[1]].append(cloth[0]) # 해당 key에 item 넣기
else: # 종류가 key에 없으면
closet[cloth[1]] = [cloth[0]] # key 만들고 item 넣기
for i in closet.values():
answer *= len(i) + 1 # value + 1 곱하기
return answer-1
실행 결과
4. 회고
옷의 종류는 얼굴, 상의, 하의, 겉옷으로 나뉘어져 있기 때문에 이를 딕셔너리에 저장하는 방식으로 문제를 해결했다. 모든 옷의 종류가 주어지는 게 아니기 때문에 옷의 종류를 보고 key로 등록하고, 처음 보는 옷의 종류이면 새롭게 key로 정의한다. 옷의 종류별로 옷의 개수를 보고 조합의 수를 구한다.
5. Github
https://github.com/iihye/Algorithm/blob/main/Programmers/spy_camouflage.py
728x90
'CodingTest > Programmers' 카테고리의 다른 글
[프로그래머스]전화번호 목록(Python) (0) | 2022.11.06 |
---|---|
[프로그래머스]체육복(Python) (0) | 2022.10.30 |
[프로그래머스]행렬의 곱셈(Python) (0) | 2022.10.24 |
[프로그래머스]N개의 최소공배수(Python) (0) | 2022.10.20 |
[프로그래머스]구명보트(Python) (0) | 2022.10.04 |
Comments