Created
November 14, 2012 17:08
-
-
Save romanmt/4073373 to your computer and use it in GitHub Desktop.
Coin change kata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _ = require 'underscore' | |
| changeForDenomination = (denomination, amount) -> | |
| to = Math.floor(amount / denomination) | |
| if to > 0 then [1..to].map () -> denomination else [] | |
| makeChange = (amount, denominations = [25, 10, 5 , 1]) -> | |
| denomination = _.first denominations | |
| if denomination | |
| changeForDenomination(denomination, amount) | |
| .concat(makeChange amount % denomination, _.rest denominations) | |
| else | |
| [] | |
| describe 'makeChange()', -> | |
| it 'returns [1] for 1', -> | |
| expect(makeChange 1).toEqual [1] | |
| it 'returns [1, 1] for 2', -> | |
| expect(makeChange 2).toEqual [1, 1] | |
| it 'returns [5] for 5', -> | |
| expect(makeChange 5).toEqual [5] | |
| it 'returns [5, 1] for 6', -> | |
| expect(makeChange 6).toEqual [5, 1] | |
| it 'returns [10] for 10', -> | |
| expect(makeChange 10).toEqual [10] | |
| it 'returns [10, 5, 1] for 16', -> | |
| expect(makeChange 16).toEqual [10, 5, 1] | |
| it 'returns [10, 5, 1, 1] for 17', -> | |
| expect(makeChange 17).toEqual [10, 5, 1, 1] | |
| it 'returns [25] for 25', -> | |
| expect(makeChange 25).toEqual [25] | |
| it 'returns [25, 25, 10, 5, 1, 1] for 67', -> | |
| expect(makeChange 67).toEqual [25, 25, 10, 5, 1, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment