-
Notifications
You must be signed in to change notification settings - Fork 0
/
changer.rb
55 lines (44 loc) · 1.09 KB
/
changer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
describe "Changer" do
def combine amount, change
change.inject(amount,:+)
end
def only_coins change
(change - [1,5,10,25]).empty?
end
it "should have change and amount add to a dollar" do
(1..99).each do |x|
change = changer(x)
combine(x, change).should == 100
end
end
it "should give change in quarters, dimes, nickels and pennies" do
(1..99).each do |x|
change = changer(x)
only_coins(change).should == true
end
end
it "give back everything for amounts >= a dollar" do
(100..105).each do |x|
change = changer(x)
combine(0,change).should == x
change.size.should == 1
end
end
it "should give an error for an amount <= zero" do
(-5..0).each do |x|
lambda {changer(x)}.should raise_error
end
end
end
TOTAL = 100
COINS = [25,10,5,1]
def changer amount
raise RuntimeError if ( amount <= 0 )
return [amount] if ( amount >= TOTAL )
result = []
returned = (TOTAL - amount)
COINS.each do |x|
returned -= ( (returned/x).times { result.push(x)}) * x
end
result
end