Skip to content

Instantly share code, notes, and snippets.

@Ern-st
Created April 10, 2019 19:00
Show Gist options
  • Save Ern-st/4f24368528e6b29396debf5c4edd7eab to your computer and use it in GitHub Desktop.
Save Ern-st/4f24368528e6b29396debf5c4edd7eab to your computer and use it in GitHub Desktop.
byttepenge.py
#!/usr/bin/env python3
units = [0.5, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000]
def calculate(price, paid):
print("pris: {} betalt: {}".format(price, paid))
byttepenge = paid - price
afrundet = round(byttepenge * 2, 0)/2
if byttepenge != afrundet:
print("afrundet fra {} til {}".format(byttepenge, afrundet))
print("byttepenge: {}".format(afrundet))
returned = returnUnits(afrundet, [])
print("mønter/sedler retur: {}\n".format(returned))
def returnUnits(amount, returned = []):
for unit in units[::-1]:
if amount in units:
returned.append(amount)
return returned
left = unit - amount
if left <= 0:
returned.append(unit)
return returnUnits(abs(left), returned)
calculate(0.15, 10.0)
calculate(39, 80)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment