Last active
August 29, 2015 14:24
-
-
Save rr-paras-patel/9fb1e4b3b6ffb92dacd5 to your computer and use it in GitHub Desktop.
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
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|") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Input : -123$☺️
Output
Input : 0.001$☺️
Output
Input : 0.001abc$☺️
Output