python - How to filter out the unique combinations from a list of subsets -
i trying come script to implement subset sum prob, first script of this post. so, running script, this:
maci:python sant$ ./subsetsum.py -n3,4,5,6,7,8,9,3,4,5 -t12 [3, 4, 5] => 12 [3, 4, 5] => 12 [3, 5, 4] => 12 [3, 6, 3] => 12 [3, 9] => 12 [3, 4, 5] => 12 [4, 5, 3] => 12 [4, 8] => 12 [4, 3, 5] => 12 [5, 7] => 12 [5, 3, 4] => 12 [7, 5] => 12 [8, 4] => 12 [9, 3] => 12 [3, 4, 5] => 12
which working fine. how filter out unique subset? in result, 1, 2 , 15 same , there other 6, combination of [3,4,5]. how print 1 in stead of of them? cheers!!
ps. know q not reflecting want, feel free improve it.
instead of having numbers in list multiple times add tuples of (number, multiplicity) input become [(3, 2), (4, 2), (5, 2), (6, 1), (7, 1), (8, 1), (9, 1)]
.
this makes easy create subsets without duplicates. somethings like:
for in n[1]: subset_sum_recursive(remaining, target, partial + * [n[0]])
alternatively might easier keep not "partial" list "discarded" list. can check
if(n not in discarded) subset_sum_recursive(remaining,target,partial + [n])
Comments
Post a Comment