Created
August 23, 2013 05:14
-
-
Save uris77/6315757 to your computer and use it in GitHub Desktop.
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
def countChange(money: Int, coins: List[Int]): Int = { | |
def compare(a: Int, b: Int) = { | |
if (a == b) "==" | |
else if (a > b) ">" | |
else "<" | |
} | |
def calculateChange(sum: Int, coins: List[Int]): Int = { | |
if (coins.isEmpty) 0 | |
else | |
compare(sum + coins.head, money) match{ | |
case "==" => 1 + calculateChange(sum, coins.tail) | |
case ">" => calculateChange(sum, coins.tail) | |
case _ => calculateChange(coins.head + sum, coins) + calculateChange(sum, coins.tail) | |
} | |
} | |
calculateChange(0, coins) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment