Skip to content

Instantly share code, notes, and snippets.

@dbousamra
Last active December 11, 2015 20:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dbousamra/4653938 to your computer and use it in GitHub Desktop.
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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment