Created
March 17, 2012 02:50
-
-
Save koriroys/2054557 to your computer and use it in GitHub Desktop.
Change Kata
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 Changer | |
attr_accessor :change | |
def initialize | |
@change = [] | |
end | |
def make_change(num) | |
changed = [["q", 25], | |
["d", 10], | |
["n", 5], | |
["p", 1]] | |
array = build_change_count_array(changed, num) | |
build_change_array(array) | |
end | |
def build_change_count_array(changed, num) | |
array = [] | |
changed.each do |arg| | |
array << [num/arg[1], arg[0]] | |
num = num % arg[1] | |
end | |
array | |
end | |
def build_change_array(array) | |
array.each do |coin| | |
add_to_change(coin[0], coin[1]) | |
end | |
end | |
def add_to_change(repeats, change_character) | |
repeats.times { @change << change_character} | |
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 'spec_helper' | |
require_relative '../lib/change' | |
describe Changer do | |
let(:money) {Changer.new} | |
it "returns a penny" do | |
money.make_change(1) | |
money.change.should == %w(p) | |
end | |
it "returns two pennies" do | |
money.make_change(2) | |
money.change.should == %w(p p) | |
end | |
it "returns a nickel" do | |
money.make_change(5) | |
money.change.should == %w(n) | |
end | |
it "returns a nickel and a penny" do | |
money.make_change(6) | |
money.change.should == %w(n p) | |
end | |
it "returns a dime" do | |
money.make_change(10) | |
money.change.should == %w(d) | |
end | |
it "returns a dime, nickel, and two pennies" do | |
money.make_change(17) | |
money.change.should == %w(d n p p) | |
end | |
it "returns a quarter" do | |
money.make_change(25) | |
money.change.should == %w(q) | |
end | |
it "returns two quarters and a penny" do | |
money.make_change(51) | |
money.change.should == %w(q q p) | |
end | |
it "returns a three quarters, two dimes, and four pennies"do | |
money.make_change(99) | |
money.change.should == %w(q q q d d p p p p) | |
end | |
end |
Loved that last screencast -- super simple!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I asked Josh Cheek to critique my implementation. He sat down and made me a personal screencast. What a standup guy! Screencast here:
http://vimeo.com/38729506
followup screencast here, with Josh's roommate, Michael:
http://vimeo.com/38732037