Skip to content

Commit

Permalink
Add minimum coins.
Browse files Browse the repository at this point in the history
  • Loading branch information
rayhamel committed Apr 10, 2015
1 parent f3b77b3 commit 29f1a1d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions minimum_coins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def minimum_coins(number)
minimum = 0
[100, 50, 25, 10, 5, 1].each do |denom|
return minimum if number == 0
minimum += number / denom
number %= denom
end
minimum
end
9 changes: 9 additions & 0 deletions ruby-kata-minimum-coins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Write a method `def minimum_coins(number)` which takes a number as an argument (ranging from 1 to 250) and returns the minimum number of coins needed to calculate `number`. Coins here represent the following values: 1, 5, 10, 25, 50 and 100. Consider the following examples:

* The minimum number of coins to generate `35` would be 2: 10 and 25.
* The minimum number of coins to generate `37` would be 4: 1, 1, 10, 25.
* The minimum number of coins to generate `101` would be 2: 1 and 100.

Notes:

* Do the assignment with TDD if you want some TDD practice (you likely will need another file in addition to `code.rb` for your test suite if you choose to do this)!
9 changes: 9 additions & 0 deletions spec/_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative '../caesar_cipher'
require_relative '../duplicate_element_counter'
require_relative '../minimum_coins'
require_relative '../minimum_consecutive_integers'
require_relative '../optimal_guesser'
require_relative '../palindrome'
Expand Down Expand Up @@ -85,6 +86,14 @@
end
end

describe '#minimum_coins' do
it 'returns the minimum number of coins needed' do
expect(minimum_coins(35)).to eq 2
expect(minimum_coins(37)).to eq 4
expect(minimum_coins(101)).to eq 2
end
end

describe '#palindrome?' do
it 'correctly determines if a string is a palindrome' do
expect(palindrome?('A Santa at NASA')).to eq true
Expand Down

0 comments on commit 29f1a1d

Please sign in to comment.