The following problem is often called by several names, and has plenty of literature available. Unfortunately, I'm a little new to Python, and could use a little help applying the solution to my case.
I have a pandas dataframe containing ~40,000 rows, so optimization is probably a factor. The dataframe contains several columns of object codes, and a resulting column of dollar amounts. I would like to prove that a particular subset of these dollar amounts total a given value. In other words, I would like to prove the following:
IN:
Target: $11.72
Code1 Code2 Code3 Amount
RG22 331 ZAV $2.00
XG11 542 TAM $4.23
RG22 117 GEE $6.81
RG76 956 ZXA $2.91
ZZ99 223 TTQ $11.99
BW32 454 PBC $9.35
OUT:
Code1 Code2 Code3 Amount
RG22 331 ZAV $2.00
RG22 117 GEE $6.81
RG76 956 ZXA $2.91
Most solutions (including this great solution, code below) only accept and return lists of values. I need a solution which would reproduce the object codes as well. Please advise, and thank you!
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