Skip to content

Instantly share code, notes, and snippets.

@uris77
Created August 23, 2013 05:14
Show Gist options
  • Save uris77/6315757 to your computer and use it in GitHub Desktop.
Save uris77/6315757 to your computer and use it in GitHub Desktop.
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