Created
February 1, 2013 03:15
-
-
Save anonymous/4688880 to your computer and use it in GitHub Desktop.
The evolution of a change calculator
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
######## Version 1 - brute force | |
amountOfChange = 2.30 | |
numQuarters = amountOfChange//.25 | |
remainingChange = amountOfChange%.25 | |
numDimes = remainingChange//.1 | |
remainingChange = remainingChange%.1 | |
numNickels = remainingChange//.05 | |
remainingChange = remainingChange%.05 | |
numPennies = remainingChange | |
print "From $%s, there are %s quarters, %s dimes, %s nickels, %s pennies." %(amountOfChange, numQuarters, numDimes, numNickels, numPennies) | |
# Output: From $2.3, there are 9.0 quarters, 0.0 dimes, 0.0 nickels, 0.05 pennies. | |
######## Version 2 - wrap it in a function | |
def change_calculator(amountOfChange): | |
numQuarters = amountOfChange//.25 | |
remainingChange = amountOfChange%.25 | |
numDimes = remainingChange//.1 | |
remainingChange = remainingChange%.1 | |
numNickels = remainingChange//.05 | |
remainingChange = remainingChange%.05 | |
numPennies = remainingChange | |
print "From $%s, there are %s quarters, %s dimes, %s nickels, %s pennies." %(amountOfChange, numQuarters, numDimes, numNickels, numPennies) | |
change = 2.30 | |
change_calculator(change) | |
# Output: From $2.3, there are 9.0 quarters, 0.0 dimes, 0.0 nickels, 0.05 pennies. | |
######## Version 3 - implement loops | |
def change_calculator(amountOfChange): | |
quarter = .25 | |
dime = .1 | |
nickel = .05 | |
penny = .01 | |
coins = [quarter, dime, nickel, penny] | |
print "The amount of change is: ", amountOfChange | |
for coin in coins: | |
numCoin = amountOfChange//coin | |
amountOfChange = amountOfChange%coin | |
print "There are %s %s pieces." %(numCoin, coin) | |
change = 2.30 | |
change_calculator(change) | |
''' | |
Output: | |
The amount of change is: 2.3 | |
There are 9.0 0.25 pieces. | |
There are 0.0 0.1 pieces. | |
There are 0.0 0.05 pieces. | |
There are 4.0 0.01 pieces. | |
------> Notice how many pennies there are... This isn't right! | |
There are a number of things we can try, but as we're dealing with floating point numbers, adding a little bit (smaller than any coin we have), may fix our 'rounding' error. | |
Increasing the amount of change ever so slightly fixes the problem -> | |
The amount of change is: 2.30001 | |
There are 9.0 0.25 pieces. | |
There are 0.0 0.1 pieces. | |
There are 1.0 0.05 pieces. | |
There are 0.0 0.01 pieces. | |
But that's not the most elegant solution. We need a better approach! | |
''' | |
######## Version 4 - fix errors (add round(amountOfChange, 2)) | |
def change_calculator(amountOfChange): | |
quarter = .25 | |
dime = .1 | |
nickel = .05 | |
penny = .01 | |
coins = [quarter, dime, nickel, penny] | |
print "The amount of change is: ", amountOfChange | |
for coin in coins: | |
numCoin = amountOfChange//coin | |
amountOfChange = amountOfChange%coin | |
amountOfChange = round(amountOfChange, 2) | |
print "There are %s %s pieces." %(numCoin, coin) | |
change = 2.30 | |
change_calculator(change) | |
''' | |
Output: | |
The amount of change is: 2.3 | |
There are 9.0 0.25 pieces. | |
There are 0.0 0.1 pieces. | |
There are 1.0 0.05 pieces. | |
There are 0.0 0.01 pieces. | |
Hey! We fixed it! | |
''' | |
######## Version 5 - Make the output look nicer - add a coin name | |
def change_calculator(amountOfChange): | |
quarter = .25 | |
dime = .1 | |
nickel = .05 | |
penny = .01 | |
coins = [("Quarter", quarter), ("Dime", dime), ("Nickel", nickel), ("Penny", penny)] | |
print "The amount of change is: ", amountOfChange | |
for name, coin in coins: | |
numCoin = amountOfChange//coin | |
amountOfChange = amountOfChange%coin | |
amountOfChange = round(amountOfChange, 2) | |
print "There are %s %s." %(numCoin, name) | |
change = 2.30 | |
change_calculator(change) | |
''' | |
Output: | |
The amount of change is: 2.3 | |
There are 9.0 Quarter. | |
There are 0.0 Dime. | |
There are 1.0 Nickel. | |
There are 0.0 Penny. | |
''' | |
######## Version 6 - Make the output look nicer - make the correct things plural | |
def change_calculator(amountOfChange): | |
quarter = .25 | |
dime = .1 | |
nickel = .05 | |
penny = .01 | |
coins = [("Quarter", quarter), ("Dime", dime), ("Nickel", nickel), ("Penny", penny)] | |
print "The amount of change is: ", amountOfChange | |
for name, coin in coins: | |
numCoin = amountOfChange//coin | |
if numCoin > 0: | |
amountOfChange = amountOfChange%coin | |
amountOfChange = round(amountOfChange, 2) | |
if numCoin > 1: | |
# Note the special case for plural with penny | |
if name[-1] == "y": | |
name = name[:-1] + "ies" | |
else: | |
name = name+"s" | |
print "There are %s %s." %(numCoin, name) | |
change = 2.30 | |
change_calculator(change) | |
''' | |
Output: | |
The amount of change is: 2.3 | |
There are 9.0 Quarters. | |
There are 1.0 Nickel. | |
''' | |
######## Version 7 - Make the output look nicer - take care of is/are | |
def change_calculator(amountOfChange): | |
quarter = .25 | |
dime = .1 | |
nickel = .05 | |
penny = .01 | |
coins = [("Quarter", quarter), ("Dime", dime), ("Nickel", nickel), ("Penny", penny)] | |
print "The amount of change is: ", amountOfChange | |
for name, coin in coins: | |
numCoin = amountOfChange//coin | |
if numCoin > 0: | |
amountOfChange = amountOfChange%coin | |
amountOfChange = round(amountOfChange, 2) | |
isAre = "is" | |
if numCoin > 1: | |
isAre = "are" | |
# Note the special case for plural with penny | |
if name[-1] == "y": | |
name = name[:-1] + "ies" | |
else: | |
name = name+"s" | |
print "There %s %s %s." %(isAre, numCoin, name) | |
change = 2.30 | |
change_calculator(change) | |
''' | |
Output: | |
The amount of change is: 2.3 | |
There are 9.0 Quarters. | |
There is 1.0 Nickel. | |
''' | |
######## Version 8 - Make the output look nicer - convert numCoin to int | |
def change_calculator(amountOfChange): | |
quarter = .25 | |
dime = .1 | |
nickel = .05 | |
penny = .01 | |
coins = [("Quarter", quarter), ("Dime", dime), ("Nickel", nickel), ("Penny", penny)] | |
print "The amount of change is: ", amountOfChange | |
for name, coin in coins: | |
numCoin = amountOfChange//coin | |
if numCoin > 0: | |
amountOfChange = amountOfChange%coin | |
amountOfChange = round(amountOfChange, 2) | |
isAre = "is" | |
if numCoin > 1: | |
isAre = "are" | |
# Note the special case for plural with penny | |
if name[-1] == "y": | |
name = name[:-1] + "ies" | |
else: | |
name = name+"s" | |
print "There %s %s %s." %(isAre, int(numCoin), name) | |
change = 2.30 | |
change_calculator(change) | |
''' | |
Output: | |
The amount of change is: 2.3 | |
There are 9 Quarters. | |
There is 1 Nickel. | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment