Write a function which takes in a list of positive integers and returns a list of integers approximating the percent of total for the corresponding integer in the same position.
All integers in the return list must exactly add up to 100. You can assume the sum of integers passed in is greater than 0. How you want to round or truncate decimals is up to you as long as any single resulting integer returned as a percentage is off by no more than 1 in either direction.
p([1,0,2]) -> [33,0,67] or [34,0,66]
p([1000,1000]) -> [50,50] or [49,51] or [51,49]
p([1,1,2,4]) -> [12,12,25,51] or [13,12,25,50] or [12,13,25,50] or [13,13,25,49] or [13,12,26,49] or [12,13,26,49] or [12,12,26,50]
p([0,0,0,5,0]) -> [0,0,0,100,0]
This is code-golf, so shortest code in bytes wins!
[13,13,25,49]
is also ok for the third example. \$\endgroup\$p([2,2,2,2,2,3])
. It has many possible legal answers, but not all2
's can be mapped to the same value. This eliminates many overly-simple algorithms that work on all the previous test cases because the rounding isn't too bad. \$\endgroup\$p([1000,1000]) -> [49,51]
? \$\endgroup\$