Skip to content

Instantly share code, notes, and snippets.

@vdupain
Created April 8, 2013 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdupain/5334949 to your computer and use it in GitHub Desktop.
Save vdupain/5334949 to your computer and use it in GitHub Desktop.
Exercise 3: Counting Change
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