public
Last active

  • Download Gist
gistfile1.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
object Challenge119Easy {
 
def change(amount: Double) = {
val coins = Map[Int, String](25 -> "Quarter", 10 -> "Dime", 5 -> "Nickel", 1 -> "Penny")
def recurse(m: Int, cs: List[Int] = coins.keys.toList): List[Int] = cs match {
case Nil => Nil
case x :: rs => List.fill(m/x)(x) ::: recurse(m % x, rs)
}
val result = recurse((amount*100).toInt)
coins.filterKeys(k => result.contains(k))
.map { case (k, v) => v + "s: " + result.count(_ == k)}
.mkString("\n")
}
 
def main(args: Array[String]) = println(change(1.23))
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.