Last active
December 9, 2015 20:39
-
-
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
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
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 |
I like your original version better :P
@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
Original version written in 2001 :) My first ever program I think