Balance 2 sets of values item by item

Question:

my question may seem similar to others but I haven’t found anything remotely close to what I need yet (I’m still searching around in case). I have a project (details on the project aren’t necessary) when I find myself with a finite list of negative values and 2 other finite sets of positive values (floating point numbers). Take the example sets below:

negative_values = [-0.246497, -0.341068]
positive_values_1 = [0.522148, 0.923764, 0.112573, 0.401668]
positive_values_2 = [0.281474]

These sets can range in size (empty to size N). What I need to do is take the values in the first positive set and try (might not be possible depending on the set) to make the values in the negative set 0 by adding them together value by value.

If it isn’t possible with only the first, then use the second set and if it still isn’t possible, then make as many of the values in ‘negative_values’ 0 as possible.

With the above sets, this would render something like this:

def cancelOut(negative_values, positive_values_1, positive_values_2):
    # Algorith doing something

new_negative_values = [0, 0]
new_positive_values_1 = [0, 0.858347, 0.112573, 0.401668]
new_positive_values_2 = [0.281474]

What has happened is that the first values in positive_values_1 made the first value in negative_value 0 and increased the second one closer to 0. Then part of the second value in positive_values_1 made the second value in negative_value 0 and then the algorithm is done. I simply want to add the values in the positive sets to the values in negative_value one by one until they are all 0 or until the positive_value sets are all 0.

I don’t know if there’s a simple way to do this or if I need to specifically iterate through each set value by value and calculate what I want.

Thanks in advance everyone!

Asked By: Max Michel

||

Answers:

Here is a place to start – it will be an exercise in logic and keeping track of things.

a = negative_values
b = positive_values_1
c = positive_values_2

Start with the first items of a and b.

add the a and b item together  
   if the result is > 0 change a's item to 0  
      and get a's next item to use in the next iteration  
          use the result for the b item in the next iteration
   if the result is 0 change a and b's item to 0  
       and get the next items from a and b to use in the next iteration  
   if the result is < 0 change b's item to zero  
       and get b's next item for use in the next iteration
           use the result for the a item in the next iteration   
if you get a StopIteration for a you are done
    make the current item/index of b (or c) = the result
if you get a StopIteration for b, switch to c
if you get a StopIteration for c you are done
    make the current item/index of a = the result
repeat  
Answered By: wwii
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.