Let P=[P1, P2, ..., Pk]
be k
positive integers and let T
be a positive integer. I would like to generate all combinations that sum up
This is like the subset sum problem (mentioned in the comments), except that's about finding numbers summing equal to a target. You want to find numbers summing to a total less-than-or-equal to the target.
Nonetheless, a similar approach with dynamic programming can be used:
def subset_sum(vals, target=0):
sums = {0: [()]} # key=sum, value=list of subsets for the sum
if target in sums:
yield from sums[target] # annoying base case
for val in vals:
items = sums.items() # don't change dict size during iteration
sums = dict(items)
for prev_sum, prev_subsets in items:
sum_ = prev_sum + val
subsets = [s + (val,) for s in prev_subsets]
sums[sum_] = sums.get(sum_, []) + subsets
if sum_ <= target:
yield from subsets
Demo:
>>> for subset in subset_sum([1, 2, 3], target=4):
... print(*subset, sep='+')
...
1
2
1+2
3
1+3