How can I get the most optimal combinations of numbers (dimensions from an existing list) in order to reach a target (already known)

I know how to create a code in python which actually can allow me to reach the desired number (target) with strings as input, now, what I don’t know how to do is to make this work:

I have a list with dimensions (0.15,0.30,0.45,0.60,0.90,1.20) and I know my target is 0.80.
How can I test the possible combinations of the existing dimensions to make sure that the difference between the target and the optimal combination is minimal?

I am new to Dynamo, so I have not been able to find how to reach the result with Nodes, and as I said before, I though about this on python, but it does not take me to the place i want.

def subset_sum(numbers, target, partial=[]):
s = sum(partial)

# this check if the partial sum is equals to target
if s == target: 
    print "sum(%s)=%s" % (partial, target)
if s >= target:

for i in range(len(numbers)):
    n = numbers[i]
    remaining = numbers[i+1:]
    subset_sum(remaining, target, partial + [n])

You could also use the standard Python itertools module and one of its combinatorial generation method. I posted a few examples of this here (using Python within Grasshopper, but should work exactly the same, except you’ll need to reference the Standard Library):

Hello AndersDeleuran! I tried to script my code using python and it’s now working exactly as i intended, the thing is, I don’t know how to get it to work in Dynamo…

you could do this with custom nodes and recursion (though I would not) - if you decide to try you must use the scopeIf node.

you can use a loopWhile node and think functionally.

you can do this iteratively (or recursively) with designScript using imperative code and just implement what you have here in DS.

you can use nodes to calculate all differences for some set of permutations or sets and then simply sort…

you can write a zero touch c# node which you generalize.

you can use python in dynamo (not sure if you are aware of that)

Hello Michael_Kirschner2, I am trying to do it in python and it is currently working when I try to print the results in the console, but, the bad part is that when i try to reproduce it on dynamo, I am having problems defining the variables, would you please help me correct it?

I posted what i am doing on this forum on this particular thread

The simple way to do this is to not write your python in a console. Retype it line by line To find the error.

This will ensure that you only use functions available in the python version used by dynamo (2.7) as well.