Skip to content

Instantly share code, notes, and snippets.

@PurpleBooth
Created September 24, 2019 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PurpleBooth/cb6c8e286ffebafa4ecd2d355ffaf6e1 to your computer and use it in GitHub Desktop.
Save PurpleBooth/cb6c8e286ffebafa4ecd2d355ffaf6e1 to your computer and use it in GitHub Desktop.

Coin Changer Kata

You've just created a virtual vending machine that will dispense widgets of programming goodness when a user puts money into the machine. The machine should dispense the proper change. You now need the programming logic to determine which coins to dispense.

Write a program that will correctly determine the least number of coins to be given to the user such that the sum of the coins' value would equal the correct amount of change.

Interface

Parameters

  1. change amount
  2. coin denomination array (ie [1, 5, 10, 25, 100] )

Return

number of coins array (ie [1, 0, 1, 0, 0] would represent $0.11

For example

An input of 15 with [1, 5, 10, 25, 100] should return fifteen cents or [0, 1, 1, 0, 0] An input of 40 with [1, 5, 10, 25, 100] should return two dimes or [0, 1, 1, 1, 0]

coin_change(15, [1, 5, 10, 25, 100])
# => [0, 1, 1, 0, 0]

Description

Part of the challenge with this exercise is to think through your testing strategy. Do you test every possible input for the change amount, or do you test specific boundary cases?

Use Test Driven Development (TDD) to solve this problem. Start writing tests, write enough code to get the tests to pass, and then refactor your code. Allow your design to emerge from writing tests; don't try to solve the problem first.

If you prefer to return a hash instead of am array, that's ok too. {1 => 1, 5 => 0, 10 => 1, 25 => 0, 100 => 0} equals $0.11

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