Skip to content

Instantly share code, notes, and snippets.

@marsbomber
Last active December 9, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marsbomber/4325345 to your computer and use it in GitHub Desktop.
Save marsbomber/4325345 to your computer and use it in GitHub Desktop.
Rework on uni year one assignment question. Given any amount in cents, calculate changes
module ChangeMachine
class IdiotEncountered < StandardError; end
AVAILABLE_UNITS = [10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5]
def self.show_me_the_money(dollar_in_cents)
raise IdiotEncountered, "You are an idiot, no such amount in AUS..." unless dollar_in_cents % 5 == 0
dispense(dollar_in_cents)
end
def self.dispense(dollar_in_cents)
return {} if dollar_in_cents == 0
unit = AVAILABLE_UNITS.select{|u| u <= dollar_in_cents}.max
{unit => (dollar_in_cents / unit)}.merge(dispense(dollar_in_cents % unit))
end
end
puts ChangeMachine.show_me_the_money(37500).inspect
@tommyli
Copy link

tommyli commented Dec 18, 2012

I like your original version better :P

@marsbomber
Copy link
Author

@tommyli that's the exact version I submitted ... Now thinking about it, tutoring is hard work. It's like a torture EVERY single time you mark a year one student assignment... well most of them anyway

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