# Task: # Write a function that returns the number of combinations # of a given set of weights for a given weight def solution(kit_of_wt, weight): unique_wt = sorted(list(set(kit_of_wt)), reverse=True) num_of_wt = {x: kit_of_wt.count(x) for x in unique_wt} cur_num_of_wt = {x: 0 for x in num_of_wt} count = 0 def sup_func(wt_gain, m, num_of_wt, cur_num_of_wt): nonlocal count temp = {x: num_of_wt[x] for x in num_of_wt if x <= m and num_of_wt[x]} for wt in temp: num_of_wt[wt] -= 1 cur_num_of_wt[wt] += 1 wt_gain1 = wt_gain - wt if not wt_gain1: # print(cur_num_of_wt) count += 1 else: sup_func(wt_gain1, wt, num_of_wt, cur_num_of_wt) num_of_wt[wt] += 1 cur_num_of_wt[wt] -= 1 sup_func(weight, weight, num_of_wt, cur_num_of_wt) return count kit_of_wt = [8, 7, 3, 2, 2, 1, 1, 1] weight = 10 print(solution(kit_of_wt, weight))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question