How To Make Combination, If Any One Of The Element Exists That Can Be Added To Make Sum?
To find all possible combinations that can be added to make given sum. Combinations can be formed with multiple elements and also if any single element exists. Input: l1 = [9,1, 2,
Solution 1:
You can use dictionary comprehension
from itertools import combinations
l1 = [9,1, 2, 7, 6, 1, 5]
target = 8
for i in range(len(l1)):
for c in combinations(l1,i):
if sum(c) == target:
res = { i:x for i,x in enumerate(c)}
print(res)
Post a Comment for "How To Make Combination, If Any One Of The Element Exists That Can Be Added To Make Sum?"