Till Payout: Greedy Bills and Coins, Listed A to Z
Greedy & IntervalsAuto-graded · 25 test casesLast updated
Parse decimal money strings exactly into cents, detect underpayment or exact payment, then greedily break the change into named bills and coins and output them sorted alphabetically.
Till Payout
A shop's till receives two amounts written as decimal strings: the amount_due for a purchase and the amount_paid handed over by the customer. You must work out which bills and coins the cashier should hand back.
To avoid floating-point rounding errors, do all money arithmetic with integers (for example, by converting each amount to a whole number of cents).
Available denominations
| Name | Value in cents |
|---|---|
PENNY | 1 |
NICKEL | 5 |
DIME | 10 |
QUARTER | 25 |
HALF DOLLAR | 50 |
ONE | 100 |
TWO | 200 |
FIVE | 500 |
TEN | 1000 |
TWENTY | 2000 |
FIFTY | 5000 |
ONE HUNDRED | 10000 |
Names containing a space (HALF DOLLAR, ONE HUNDRED) are single names; the space is part of the name.
Task
Implement a function that takes amount_due and amount_paid and returns a string:
- If
amount_paidis less thanamount_due, return"ERROR". - If the two amounts are equal, return
"ZERO". - Otherwise, the change is
amount_paid - amount_due. Build it greedily: repeatedly take the largest denomination that does not exceed what is still owed, until nothing remains. Return the names of all items handed back, in alphabetical (lexicographic) order, separated by commas with no extra spaces. A denomination used several times appears that many times.
Input format
Each argument is a string holding a non-negative decimal number with at most two digits after the decimal point. It may have no decimal point at all ("20"), a single fractional digit ("0.5" means 50 cents), or two fractional digits ("15.94").
Output format
One string: "ERROR", "ZERO", or the comma-separated, alphabetically sorted list of denomination names.
Constraints
0.00 <= amount_due, amount_paid <= 10000.00- Each input consists only of digits and at most one decimal point.
- At most two digits follow the decimal point.
- Sorting is plain string ordering on the names (so, for example,
ONEcomes beforeONE HUNDRED).
Examples
Example 1
Input: make_change('15.94', '16.00')
Output: 'NICKEL,PENNY'
Example 2
Input: make_change('1.02', '1.24')
Output: 'DIME,DIME,PENNY,PENNY'
Example 3
Input: make_change('50.00', '40.00')
Output: 'ERROR'
Hints
Hint 1
Do not use float. Split each string at the decimal point, multiply the whole part by 100, and add the fractional part padded on the right to exactly two digits (so "0.5" is 50 cents, not 5).
Hint 2
Check for underpayment and exact payment after converting to cents, before doing any breakdown.
Hint 3
Go through the denominations from largest to smallest and use divmod to get how many of each fit; then sort the collected names only at the end, since dispensing order and output order differ.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
- Convert to cents exactly. Split each string at the decimal point. Multiply the whole part by 100. Pad the fractional part on the right with zeros, keep the first two characters, and add it as an integer. This turns
"20"into 2000,"0.5"into 50,"15.94"into 1594 and"5."into 500, with no floating-point error. - Handle the special cases. If the paid amount in cents is smaller than the due amount, return
"ERROR". If the difference is zero, return"ZERO". - Break the change greedily. Walk the denominations from the largest (10000 cents) to the smallest (1 cent). For each,
divmod(remaining, value)gives how many items of that kind to hand out and the leftover amount. Append the name that many times. - Format. Sort the collected names as ordinary strings and join them with commas.
Why it is correct
- Integer arithmetic makes the comparison and subtraction exact.
- This denomination set is canonical, so taking as many of the largest fitting denomination as possible always yields the minimum number of items. In any case the task specifies the greedy procedure, so its output is by definition the expected answer.
- Each name is added once per item dispensed, so repeats are preserved. Dispensing order (by value) is kept separate from output order (alphabetical), which is applied only at the end.
- Names with spaces are never split, since they are handled as whole strings.
Complexity
- Time: O(L) to parse the inputs, O(12) for the greedy pass, and O(k log k) to sort the k dispensed items. Since change is at most 10000.00, k is at most 100 plus a few small coins, so this is effectively constant.
- Space: O(k) for the list of items and the output string.
Reference solution
def make_change(amount_due, amount_paid):
def to_cents(text):
whole, _, frac = text.partition(".")
return int(whole) * 100 + int((frac + "00")[:2])
due = to_cents(amount_due)
paid = to_cents(amount_paid)
if paid < due:
return "ERROR"
remaining = paid - due
if remaining == 0:
return "ZERO"
denominations = [
(10000, "ONE HUNDRED"), (5000, "FIFTY"), (2000, "TWENTY"),
(1000, "TEN"), (500, "FIVE"), (200, "TWO"), (100, "ONE"),
(50, "HALF DOLLAR"), (25, "QUARTER"), (10, "DIME"),
(5, "NICKEL"), (1, "PENNY"),
]
items = []
for value, name in denominations:
count, remaining = divmod(remaining, value)
items.extend([name] * count)
items.sort()
return ",".join(items)