Created
July 21, 2016 00:26
-
-
Save zestime/7cf0d554dc7f13baf0a911a183e2ff30 to your computer and use it in GitHub Desktop.
Recursion problem from 'Functional Programming Principles in Scala'
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 = | |
money match { | |
case 0 => 1 // matched, add 1 | |
case n => if (money < 0 || coins.isEmpty) 0 // not matched, ignored | |
else countChange(money - coins.head, coins) + countChange(money, coins.tail) | |
} | |
// test cases | |
assert(calculateCoins(4, List(1,2)) == 3) | |
assert(calculateCoins(300, List(5, 10, 20, 50, 100, 200, 500)) == 1022) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment