I have a list of numbers, say,
[2,4,5,6,7]
.
I want to list all of the unique ways these numbers can be made into a sum, N
, with repeats.
For example, if N
were 8. Answers would be:
2+2+2+2
4+4
6+2
4+2+2
Order does not matter.
The closest I've got is the answer to this question: Finding all possible combinations of numbers to reach a given sum
def subset_sum(numbers, target, partial=[]): s = sum(partial)
# check if the partial sum is equals to target
if s == target:
print "sum(%s)=%s" % (partial, target)
if s >= target:
return # if we reach the number why bother to continue
for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[i+1:]
subset_sum(remaining, target, partial + [n])
if __name__ == "__main__":
subset_sum([3,9,8,4,5,7,10],15)
#Outputs:
#sum([3, 8, 4])=15
#sum([3, 5, 7])=15
#sum([8, 7])=15
#sum([5, 10])=15
How could I modify this to allow for repeats?