위장
clothes | return |
[["yellowhat", "headgear"], ["bluesunglasses", "eyewear"], ["green_turban", "headgear"]] | 5 |
[["crowmask", "face"], ["bluesunglasses", "face"], ["smoky_makeup", "face"]] | 3 |
소스
# ----------------- 통과 -----------------------
# 1
def solution1(clothes):
dic = {}
for i in clothes:
if i[1] in dic:
dic[i[1]] += 1
else:
dic[i[1]] = 2
temp = 1
for i in dic:
temp *= dic[i]
temp -= 1
return temp
# ----------------- 다른 답안 -----------------------
# 2
def solution2(clothes):
answer = {}
for _, k in clothes:
try:
answer[k] += 1
except KeyError:
answer[k] = 2
result = 1
for i in answer.values():
result *= i
return result - 1
# 3
def solution3(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
solution1 같은 경우 반복문을 통해 딕셔너리에 각 항목별로 수를 늘려나가면서 저장을 합니다. 마지막에 그 경우의 수들을 곱해주고 1을 빼준 후 리턴해줍니다. 1을 빼주는 이유는 아무것도 안 입는 경우를 배제하기 위함입니다.
solution2 같은 경우도 논리적으로 봤을 때는 solution1과 유사합니다. 다른 점을 말하자면 if문 대신 예외 처리 문을 사용한 점입니다. if문 말고도 이렇게 오류가 나올 수 있는 상황에는 예외 처리 문이 보기 좋습니다.
solution3같은 경우는 collections 모듈의 Counter() 함수와 functools의 reduce() 함수를 사용한 방법입니다. Counter() 함수를 사용하여 밸류의 내림차순 딕셔너리를 만들고 reduce() 함수를 통해 데이터를 누적하여 집계합니다. 그 후 1을 빼 리턴해주는 것이 solution3 함수의 내용입니다.
https://www.daleseo.com/python-functools-reduce/
파이썬 reduce 함수 사용법
Engineering Blog by Dale Seo
www.daleseo.com
'파이썬 > 프로그래머스' 카테고리의 다른 글
전화번호 목록 (0) | 2022.02.15 |
---|---|
완주하지 못한 선수 (0) | 2022.02.15 |
코딩테스트 연습 - 문자열 내 p와 y의 개수 (0) | 2022.02.14 |
코딩테스트 연습 - 짝수와 홀수 (0) | 2022.02.14 |
코딩테스트 연습 - 서울에서 김서방 찾기 (0) | 2022.02.14 |
위장
clothes | return |
[["yellowhat", "headgear"], ["bluesunglasses", "eyewear"], ["green_turban", "headgear"]] | 5 |
[["crowmask", "face"], ["bluesunglasses", "face"], ["smoky_makeup", "face"]] | 3 |
소스
# ----------------- 통과 -----------------------
# 1
def solution1(clothes):
dic = {}
for i in clothes:
if i[1] in dic:
dic[i[1]] += 1
else:
dic[i[1]] = 2
temp = 1
for i in dic:
temp *= dic[i]
temp -= 1
return temp
# ----------------- 다른 답안 -----------------------
# 2
def solution2(clothes):
answer = {}
for _, k in clothes:
try:
answer[k] += 1
except KeyError:
answer[k] = 2
result = 1
for i in answer.values():
result *= i
return result - 1
# 3
def solution3(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
solution1 같은 경우 반복문을 통해 딕셔너리에 각 항목별로 수를 늘려나가면서 저장을 합니다. 마지막에 그 경우의 수들을 곱해주고 1을 빼준 후 리턴해줍니다. 1을 빼주는 이유는 아무것도 안 입는 경우를 배제하기 위함입니다.
solution2 같은 경우도 논리적으로 봤을 때는 solution1과 유사합니다. 다른 점을 말하자면 if문 대신 예외 처리 문을 사용한 점입니다. if문 말고도 이렇게 오류가 나올 수 있는 상황에는 예외 처리 문이 보기 좋습니다.
solution3같은 경우는 collections 모듈의 Counter() 함수와 functools의 reduce() 함수를 사용한 방법입니다. Counter() 함수를 사용하여 밸류의 내림차순 딕셔너리를 만들고 reduce() 함수를 통해 데이터를 누적하여 집계합니다. 그 후 1을 빼 리턴해주는 것이 solution3 함수의 내용입니다.
https://www.daleseo.com/python-functools-reduce/
파이썬 reduce 함수 사용법
Engineering Blog by Dale Seo
www.daleseo.com
'파이썬 > 프로그래머스' 카테고리의 다른 글
전화번호 목록 (0) | 2022.02.15 |
---|---|
완주하지 못한 선수 (0) | 2022.02.15 |
코딩테스트 연습 - 문자열 내 p와 y의 개수 (0) | 2022.02.14 |
코딩테스트 연습 - 짝수와 홀수 (0) | 2022.02.14 |
코딩테스트 연습 - 서울에서 김서방 찾기 (0) | 2022.02.14 |