Skip to content

Instantly share code, notes, and snippets.

@f0ster
Created July 24, 2018 20:39
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 f0ster/56dbb5408a56eac97d87a92457bdd40b to your computer and use it in GitHub Desktop.
Save f0ster/56dbb5408a56eac97d87a92457bdd40b to your computer and use it in GitHub Desktop.
more scala recursive fun
/**
* Exercise 3
*/
def countChange(money: Int, coins: List[Int]): Int = {
def count(m: Int, sortedCoins: List[Int]) : Int = {
if (sortedCoins.isEmpty) 0
else if (m - sortedCoins.head == 0) 1
else if (m - sortedCoins.head < 0) 0
else countChange(m - sortedCoins.head, sortedCoins) + countChange(m, sortedCoins.tail)
}
count(money, coins.sorted)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment