Created
September 27, 2012 19:21
-
-
Save dwins/3795906 to your computer and use it in GitHub Desktop.
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
def countChange(money: Int, coins: List[Int]): Int = | |
if (money < 0 || coins.isEmpty) | |
0 | |
else if (money == 0) | |
1 | |
else | |
countChange(money - coins.head, coins) + countChange(money, coins.tail) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function is poorly named; rather than counting change, it counts the number of selections from the
coins
List which to the value ofmoney
. The assumption is that coins always have positive value, so no combination of coins can sum up to a negativemoney
value. In the case ofmoney == 0
, there is exactly one way to make the coins add up, which is to select no coins.