I have two set of lists:
first list (let us call it A) {121, 30, 70, 50}
and
a second, list of list (call it B)
{{90,10,30},{40,20,1},{10,70,20},{10,15,20}}
Following operations need to be performed to get the output list:
take the first element of list A and take first list of list B, in this case
121
and{90,10,30}
121
can be divided or stacked (or filled) as90+10+21=121
based on the list{90,10,30}
, here full of90
is used then full of10
is used then only21
of30
is used. So the output for first value is{90,10,21}
(Note: Elements are stacked from Left to right, ie., 10 starts filling only after 90 is fully filled and 30 starts filling only after 10 is fully filled)- similarly for the second items
30
and{40,20,1}
only30
out of40
is enough and then nothing of the others so,0
of20
and0
of1
, so the output is{30,0,0}
such that30+0+0=30
- For the third items
70
and{10,70,20}
full10
out of10
is used and then60
of70
and0
of20
, so the output is{10,60,0}
such that10+60+0=70
- For the fourth items
50
and{10,15,20}
full10
out of10
is used and then full15
of15
and full20
of20
, so the output is{10,15,20}
such that10+15+20=45
. NOTE: 45 is less than 50 but that is not a problem. The aim is to fully use the list to the maximum possible amounts from Left to Right and try to come close or satisfy the number.
- similarly for the second items
Some Notes:
Numbers can be float. Integers are given here for only example and explanation purpose.
The size of the lists inside list B will all be the same, in this case 3 elements.
The size of the list A and corresponding B is much larger (around 8670 items) than shown in the example.
Summary:
Inputs:{121, 30, 70, 50}
;{{90,10,30},{40,20,1},{10,70,20},{10,15,20}}
Output:{{90,10,21},{30,0,0},{10,60,0},{10,15,20}}
Fun Fact: This logic is used in real life to dispatch power plants to supply electricity demand and it is called stack dispatch model. List A is the demand at a given hour, and list B are power plants available at that hour. Output is how much of each power plant is operated to satisfy the demand.
I am looking for a simple and efficient code for this operation. Any help is appreciated.
Thank you in advance.