Wonky Coins

Catsylvanian money is a strange thing: they have a coin for every denomination (including zero!). A wonky change machine in Catsylvania takes any coin of value N and returns 3 new coins, valued at N/2, N/3 and N/4 (rounding down).

Write a method `wonky_coins(n)` that returns the number of coins you are left with if you take all non-zero coins and keep feeding them back into the machine until you are left with only zero-value coins.

Test Cases:

puts wonky_coins(1).should == 3
puts wonky_coins(5).should ==  11
# => [2, 1, 1]
# => [[1, 0, 0], [0, 0, 0], [0, 0, 0]]
# => [[[0, 0, 0], 0, 0], [0, 0, 0], [0, 0, 0]]
puts wonky_coins(6).should == 15
puts wonky_coins(0).should == 1

My solution (w/ arrays and recursion): 


Jimmy’s solution (w/o recursion): 

The idea is you draw a coin from a pouch (array).  If the coin is 0, count it.  If it’s not, redraw another coin until the pouch is empty.


Provided solution (w/ recursion):


Wonky Coins

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s