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) |
awesome!!!!!!!!
Awesome!!!!!!!!
Perfect!
If money == 0, function gave 1. But this is not logical.
This function is poorly named; rather than counting change, it counts the number of selections from the coins
List which to the value of money
. The assumption is that coins always have positive value, so no combination of coins can sum up to a negative money
value. In the case of money == 0
, there is exactly one way to make the coins add up, which is to select no coins.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an elegant solution. Thank you for this gist.