from itertools import combinations def combine_result(cargo_list, s,weight): combine_cargo = list(combinations(cargo_list, s,)) mm = [] for list_now in combine_cargo: list_now = list(list_now ) list_now .append(sum(list_now)) if list_now [-1]==weight: del list_now[-1] mm.append(list_now) print(f' количество комбинаций:{len(mm)}\n используется грузиков:{s}') return (f'количество комбинаций:{len(mm)}\nиспользуется грузиков:{s}') cargo_list = [21, 18, 19,10,30,15,25,40] #веса грузиков weight = 40 #нужный вес set_now = 2 # подставляем количество грузиков combine_result(cargo_list,set_now,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