Bill split pennies
EasyOnline Assessment
Split a restaurant bill between friends so that every cent is accounted for.
All money is in whole cents. The bill is settled like this:
- The tip is
tip_percentpercent ofsubtotal, rounded to the nearest cent, with a half cent rounded up (for example a tip of 162.5 cents becomes 163). The tip is worked out on the fullsubtotal, before the voucher. - The total to pay is
subtotal + tip - voucher. A voucher can wipe out the whole bill but never produce a refund, so a negative total becomes 0. - The total is shared among
dinerspeople as evenly as whole cents allow. Everyone pays the same base amount, rounded down, and the leftover cents are handed out one cent each to the first payers in order: person 0 pays an extra cent, then person 1, and so on, until the leftover cents run out. No one ever pays more than one extra cent.
Input:
subtotal: the food and drink total in cents.tip_percent: the tip percentage, a whole number.voucher: the value in cents of a discount voucher, possibly 0.diners: the number of people sharing the bill.
Output: a list of diners integers, the amount in cents each person pays, in order. Its sum equals the total to pay.
Example 1
- Input:
- [5000,15,0,4]
- Output:
- [1438,1438,1437,1437]
- Explanation
- A 15 percent tip on 5000 is exactly 750, so the total to pay is 5750. Four people share it: the base is 1437 each (5748 in all), and 2 cents are left over. Persons 0 and 1 pay the extra cent, so the answer is
[1438, 1438, 1437, 1437].
Example 2
- Input:
- [1250,13,500,3]
- Output:
- [305,304,304]
- Explanation
- A 13 percent tip on 1250 is 162.5 cents, rounded half up to 163, and it is worked out before the voucher. The total is 1250 + 163 - 500 = 913. Three people: the base is 304 (912 in all), and 1 cent is left over for person 0, so
[305, 304, 304].
Example 3
- Input:
- [1000,10,5000,3]
- Output:
- [0,0,0]
- Explanation
- The voucher is worth more than the bill and tip together (1000 + 100 - 5000 is negative), so the total to pay is 0 rather than a refund, and every person pays 0.
Constraints
0 <= subtotal <= 10000000 <= tip_percent <= 1000 <= voucher <= 15000001 <= diners <= 50