Created
April 10, 2019 19:00
-
-
Save Ern-st/4f24368528e6b29396debf5c4edd7eab to your computer and use it in GitHub Desktop.
byttepenge.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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