Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.47 KB | None | 0 0
  1.  
  2.  
  3. def makeChange(in: Int): Map[Int, Int] = {
  4.   def nextCoin(cur: Int) = cur match {
  5.     case 25 => 10;
  6.     case 10 => 5;
  7.     case 5 => 1;
  8.     case _ => throw new Exception(β€œBlah!”);
  9.   }
  10.   def inner(remaining: Int, coinVal: Int): Int = {
  11.     if (coinVal == 1)
  12.       1
  13.     else {
  14.       val maxCoins = remaining / coinVal;
  15.       val nextVal = nextCoin(coinVal)
  16.       (0 to maxCoins) map (inner(remaining - _ * coinVal, nextVal)) sum
  17.     }
  18.   }
  19.   inner(in, 25)
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement