Created
April 8, 2013 07:40
-
-
Save vdupain/5334949 to your computer and use it in GitHub Desktop.
Exercise 3: Counting Change
This file contains 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
package week1 | |
object countChange { | |
def countChange(money: Int, coins: List[Int]): Int = { | |
if (money > 0 && !coins.isEmpty) | |
countChange(money, coins.tail) + countChange(money - coins.head, coins) | |
else if (money == 0) 1 else 0 | |
} //> countChange: (money: Int, coins: List[Int])Int | |
countChange(0, List(1,2)) == 0 //> res0: Boolean = false | |
countChange(0, List()) == 0 //> res1: Boolean = false | |
countChange(4, List()) == 0 //> res2: Boolean = true | |
countChange(4, List(1, 2)) == 3 //> res3: Boolean = true | |
countChange(300, List(5, 10, 20, 50, 100, 200, 500)) == 1022 | |
//> res4: Boolean = true | |
countChange(301, List(5, 10, 20, 50, 100, 200, 500)) == 0 | |
//> res5: Boolean = true | |
countChange(300, List(500, 5, 50, 100, 20, 200, 10)) == 1022 | |
//> res6: Boolean = true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment