[프로그래머스]-level2-사탕 담기-Python[파이썬]

s2ul3·2022년 9월 21일
0

나의 풀이

from itertools import combinations as cb
def solution(m, weights):
    weights2 = [i for i in weights if i <= m] # 사탕들 중에서 사탕 무게가 m이하인 사탕들만 따로 리스트로 만들어준다.
    cnt = 0
    candy_cnt = len(weights)
    cb_lsts = [] # 조합 결과를 담는 리스트
    for l in range(1, candy_cnt):
        cb_lsts.append(cb(weights2, l))  # 사탕들 중에서 1개뽑기, 2개뽑기, ... 
    for cb_lst in cb_lsts:
        for i in cb_lst:
            if sum(i) == m:
                cnt += 1
    return cnt
profile
statistics & computer science

0개의 댓글