Last active
December 28, 2015 15:39
-
-
Save animatedlew/7522937 to your computer and use it in GitHub Desktop.
Finding how many combinations of change can be produced based on a total amount and list of denominations 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
val money = 300 | |
val coins = List(5, 10, 20, 50, 100, 200, 500) | |
def countChange(money: Int, coins: List[Int]): Int = { | |
if (money == 0) 1 | |
else if (money < 0 || coins.isEmpty) 0 | |
else countChange(money, coins.tail) + countChange(money - coins.head, coins) | |
} | |
// 1022 | |
countChange(money, coins) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment