Skip to content

Instantly share code, notes, and snippets.

@MaxPleaner
Last active August 29, 2015 14:02
Show Gist options
  • Save MaxPleaner/f53edc7a209372208d06 to your computer and use it in GitHub Desktop.
Save MaxPleaner/f53edc7a209372208d06 to your computer and use it in GitHub Desktop.
wonky coins
def wonky_coins(n)
return 1 if n == 0
# call wonky_coins from inside itself. This is called *recursion*.
return wonky_coins(n / 2) + wonky_coins(n / 3) + wonky_coins(n / 4)
end
# 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.
#
# Difficulty: 3/5
describe "#wonky_coins" do
it "handles a simple case" do
wonky_coins(1).should == 3
end
it "handles a larger case" do
wonky_coins(5).should == 11
# 11
# => [2, 1, 1]
# => [[1, 0, 0], [0, 0, 0], [0, 0, 0]]
# => [[[0, 0, 0], 0, 0], [0, 0, 0], [0, 0, 0]]
end
it "handles being given the zero coin" do
wonky_coins(0).should == 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment