Skip to content

Instantly share code, notes, and snippets.

@dwins
Created September 27, 2012 19:21
Show Gist options
  • Save dwins/3795906 to your computer and use it in GitHub Desktop.
Save dwins/3795906 to your computer and use it in GitHub Desktop.
def countChange(money: Int, coins: List[Int]): Int =
if (money < 0 || coins.isEmpty)
0
else if (money == 0)
1
else
countChange(money - coins.head, coins) + countChange(money, coins.tail)
@zwang
Copy link

zwang commented Nov 24, 2013

This is an elegant solution. Thank you for this gist.

@littlejin
Copy link

awesome!!!!!!!!

@frederic89
Copy link

Awesome!!!!!!!!

@cherrera20
Copy link

Perfect!

@CHEguK
Copy link

CHEguK commented Sep 19, 2020

If money == 0, function gave 1. But this is not logical.

@dwins
Copy link
Author

dwins commented Sep 19, 2020

This function is poorly named; rather than counting change, it counts the number of selections from the coins List which to the value of money. The assumption is that coins always have positive value, so no combination of coins can sum up to a negative money value. In the case of money == 0, there is exactly one way to make the coins add up, which is to select no coins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment