Skip to content

Instantly share code, notes, and snippets.

@ntijoh-daniel-berg
Created March 10, 2016 07:17
Show Gist options
  • Save ntijoh-daniel-berg/dc7653184a684c5cb744 to your computer and use it in GitHub Desktop.
Save ntijoh-daniel-berg/dc7653184a684c5cb744 to your computer and use it in GitHub Desktop.
def change_o_matic(price, amount_paid):
amount_due = amount_paid - price
if amount_due < 0:
raise ValueError("amount paid ({0}) must not be less than price ({1}) ".format(amount_paid, price))
change = {1000: 0, 500: 0, 200: 0, 100: 0,
50: 0, 20: 0, 10: 0, 5: 0, 2: 0, 1: 0 }
denominations = sorted(change.keys(), reverse=True)
for den in denominations:
while amount_due >= den:
change[den] += 1
amount_due -= den
return change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment