Skip to content

Instantly share code, notes, and snippets.

@basicxman
Created August 19, 2011 05:33
Show Gist options
  • Select an option

  • Save basicxman/1156118 to your computer and use it in GitHub Desktop.

Select an option

Save basicxman/1156118 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
def check(s):
if isinstance(s, int) or isinstance(s, float): return s
exit()
def pluralize(currency, amount):
if amount == 1: return currency
if currency == "penny":
return "pennies"
else:
return currency + "s"
def calculate(amount_due, amount_received):
coins = { 1.0: 'dollar', 0.25: 'quarter', 0.1: 'dime', 0.05: 'nickel', 0.01: 'penny' }
change = amount_received - amount_due
print "Change: $%.2f" % change
for value in reversed(sorted(coins.keys())):
quantity_of_coin = change // value
change -= quantity_of_coin * value
print "%d %s" % (quantity_of_coin, pluralize(coins[value], quantity_of_coin))
amount_due = None
amount_received = None
while True:
amount_due = check(input("Amount Due:\t $"))
amount_received = check(input("Amount Received: $"))
calculate(amount_due, amount_received)
@basicxman
Copy link
Author

loop { [($amount_due = [(print "Amount Due:\t $"), eval(gets.strip)].last), ($amount_received = [(print "Amount Received: $"), eval(gets.strip)].last), ([(puts "Change: #{(change = $amount_due - $amount_received).round(2)}"), ({ 1.0 => 'dollar', 0.25 => 'quarter', 0.1 => 'dime', 0.05 => 'nickel', 0.01 => 'penny' }.each { |value, currency| [(change -= (quantity_of_coin = (change / value).floor) * value), (puts "#{quantity_of_coin} #{quantity_of_coin == 1 ? currency : currency == "penny" ? "pennies" : currency + "s"}")] })])] }

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