Skip to content

Instantly share code, notes, and snippets.

@rr-paras-patel
Last active August 29, 2015 14:24
Show Gist options
  • Save rr-paras-patel/9fb1e4b3b6ffb92dacd5 to your computer and use it in GitHub Desktop.
Save rr-paras-patel/9fb1e4b3b6ffb92dacd5 to your computer and use it in GitHub Desktop.
from decimal import *
import sys
# Define your denominations to be used
denominations = ['500', '100', '50', '20', '10', '5', '2', '1', '0.25', '0.10', '0.05', '0.01']
# Please write this denomination list in Desending order of currency value
names = {'0.25': "quarters", '0.10': "dimes", '0.05': "nickels", '0.01': "pennies"}
# Specify special nameing for your values otherwsie default is 'dollar'
amount="5532.23$" ## we may also define command line input e.g. sys.argv[1]
# Validate amount by value type.
try:
amount = Decimal(amount.strip('$'))
except:
print("Amount provided must be numeric! You have Given '" + amount + "'")
sys.exit(2)
# Validate amount according to least value of denominations
if amount <= Decimal(denominations[len(denominations)-1]):
print("Sorry It's invalid input")
sys.exit(0)
# Calculate the minimum number of change
print( "| Qty. \t\t| Value\t\t\t\t|")
for thisdenmo in denominations:
num = Decimal(amount // Decimal(thisdenmo)) # division operation to determine no. of time we need substraction
amount -= num * Decimal(thisdenmo) # subtract the amount of change from the overall or remaining amount
if num:
print("| " + str(str(num) + '\t\t| ' + str(names[thisdenmo] +" \t\t") if thisdenmo in names else str(num) + "\t\t| $" + str(thisdenmo) + " dollar bills") + "\t|")
@rr-paras-patel
Copy link
Author

Input : 127$
Output 😄

Success time: 0.02 memory: 8480 signal:0
| Qty.      | Value             |
|  1        | $100 dollar bills |
|  1        | $20 dollar bills  |
|  1        | $5 dollar bills   |
|  1        | $2 dollar bills   |

@rr-paras-patel
Copy link
Author

Input : -123$
Output ☺️

Success time: 0.02 memory: 8528 signal:0
Sorry It's invalid input

Input : 0.001$
Output ☺️

Success time: 0.02 memory: 8528 signal:0
Sorry It's invalid input

Input : 0.001abc$
Output ☺️

Runtime error   time: 0.02 memory: 8224 signal:-1
Amount provided must be numeric! You have Given '0.001abc$'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment