Skip to content

Instantly share code, notes, and snippets.

@koriroys
Created March 17, 2012 02:50
Show Gist options
  • Save koriroys/2054557 to your computer and use it in GitHub Desktop.
Save koriroys/2054557 to your computer and use it in GitHub Desktop.
Change Kata
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
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
@koriroys
Copy link
Author

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

@trevorturk
Copy link

Loved that last screencast -- super simple!

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