Created
January 20, 2016 21:26
-
-
Save mjansen401/0a656ca68ef1343872b7 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
class CoinChanger | |
def self.change(amount) | |
change = [] | |
[25,10,5,1].each do |coin| | |
while amount >= coin | |
change << coin | |
amount -= coin | |
end | |
end | |
change | |
end | |
end |
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
require 'rspec' | |
require 'coin_changer' | |
describe(CoinChanger) do | |
[ | |
[1, [1]], | |
[2, [1,1]], | |
[3, [1,1,1]], | |
[5, [5]], | |
[6, [5,1]], | |
[7, [5,1,1]], | |
[10, [10]], | |
[11, [10,1]], | |
[15, [10,5]], | |
[20, [10,10]], | |
[25, [25]], | |
[50, [25,25]], | |
[94, [25,25,25,10,5,1,1,1,1]] | |
].each do |amount, expected| | |
it "changes #{amount} to #{expected}" do | |
expect(CoinChanger.change(amount)).to eq(expected) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment