have got input dataframe like below and a variable 'extra'
input_df
Item Space refill Min Max
Apple 0.25 12.53 0.125 0.25
Lemon 0.25 11.2 0.25 0.375
Potato 0.375 10.3 0.375 0.75
Melon 0.375 9.2 0.25 0.75
'extra' variable can be negative or positive.
Expected output_df if extra = 0.25
(this extra 0.25 space need to be splitted in the multiples of 0.125 and added to items with higher refill order. In this case, 0.125 + 0.125 = 0.25 so two items space will be updated. Also should consider Max value here, on adding space it should not exceed Max value, if happens skip that item and move to next.)
Item Space refill Min Max
Apple 0.25 12.53 0.125 0.25 #item skipped since it exceeds Max limit if extra space added
Lemon 0.375 11.2 0.25 0.375 #0.125 from extra space added here
Potato 0.5 10.3 0.375 0.75 #remaining 0.125 from extra space added here
Melon 0.375 9.2 0.25 0.75 #no more space to be added
Expected output_df if extra = -0.25
(this extra -0.25 space[which is two times of -0.125] need to be reduced from items with higher refill. Also should consider Min Value here, on reducing space it should not go below Min value, if happens skip that item and move to next.)
Item Space refill Min Max
Apple 0.125 12.53 0.125 0.25 #-0.125 from extra space reduced here
Lemon 0.25 11.2 0.25 0.375 #item skipped since it doesn't satisfy Min limit if reduced
Potato 0.375 10.3 0.375 0.75 #item skipped since it doesn't satisfy Min limit if reduced
Melon 0.25 9.2 0.25 0.75 #rem -0.125 from extra space reduced here
Any help will be appreciated. Thank You!