Last active
December 11, 2015 20:18
-
-
Save dbousamra/4653938 to your computer and use it in GitHub Desktop.
This file contains 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
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