Created
February 18, 2014 21:39
-
-
Save benneuman/9080796 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
require 'rspec' | |
# input: integer (0-100) | |
# output: hash, e.g. | |
# 5 -> {quarters: 0, dimes: 0, nickels: 1, pennies: 0} | |
# 12 -> {quarters: 0, dimes: 1, nickels: 0, pennies: 2} | |
describe "#make_change" do | |
it "1 should return 1 penny" do | |
expect(make_change(1)).to eq(quarters: 0, dimes: 0, nickels: 0, pennies: 1) | |
end | |
it "4 should return 4 pennies" do | |
expect(make_change(4)).to eq(quarters: 0, dimes: 0, nickels: 0, pennies: 4) | |
end | |
it "5 should return 1 nickel" do | |
expect(make_change(5)).to eq(quarters: 0, dimes: 0, nickels: 1, pennies: 0) | |
end | |
it "7 should return 1 nickel and 2 pennies" do | |
expect(make_change(7)).to eq(quarters: 0, dimes: 0, nickels: 1, pennies: 2) | |
end | |
it "10 should return 1 dime" do | |
expect(make_change(10)).to eq(quarters: 0, dimes: 1, nickels: 0, pennies: 0) | |
end | |
it "25 should return 1 quarter" do | |
expect(make_change(25)).to eq(quarters: 1, dimes: 0, nickels: 0, pennies: 0) | |
end | |
end | |
describe "#coin_count" do | |
it "it returns cents divided by coin_value and the remainder" do | |
expect(coin_count(76, 25)).to eq([3,1]) | |
end | |
end | |
QUARTER = 25 | |
DIME = 10 | |
NICKEL = 5 | |
def coin_count(cents, coin_value) | |
[cents / coin_value, cents % coin_value] | |
end | |
def make_change(cents) | |
quarters, remaining_cents = coin_count(cents, QUARTER) | |
dimes, remaining_cents = coin_count(remaining_cents, DIME) | |
nickels, remaining_cents = coin_count(remaining_cents, NICKEL) | |
pennies = remaining_cents | |
{quarters: coin_count(QUARTER), dimes: dimes, nickels: nickels, pennies: pennies} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment