Correctly determine the fewest number of coins to be given to a customer such that the sum of the coins' value would equal the correct amount of change.
The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.
To run the tests simply run the command sbt test
in the exercise directory.
Please see the learning and installation pages if you need any help.
Software Craftsmanship - Coin Change Kata https://web.archive.org/web/20130115115225/http://craftsmanship.sv.cmu.edu:80/exercises/coin-change-kata
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
import org.scalatest.{Matchers, FunSuite}
/** @version 1.2.0 */
class ChangeTest extends FunSuite with Matchers {
test("single coin change") {
Change.findFewestCoins(25, List(1, 5, 10, 25, 100)) should be (Some(List(25)))
}
test("multiple coin change") {
pending
Change.findFewestCoins(15, List(1, 5, 10, 25, 100)) should be (Some(List(5, 10)))
}
test("change with Lilliputian Coins") {
pending
Change.findFewestCoins(23, List(1, 4, 15, 20, 50)) should be (Some(List(4, 4, 15)))
}
test("change with Lower Elbonia Coins") {
pending
Change.findFewestCoins(63, List(1, 5, 10, 21, 25)) should be (Some(List(21, 21, 21)))
}
test("large target values") {
pending
Change.findFewestCoins(999, List(1, 2, 5, 10, 20, 50, 100)) should be (Some(List(2, 2, 5, 20, 20, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100)))
}
test("possible change without unit coins available") {
pending
Change.findFewestCoins(21, List(2, 5, 10, 20, 50)) should be (Some(List(2, 2, 2, 5, 10)))
}
test("another possible change without unit coins available") {
pending
Change.findFewestCoins(27, List(4, 5)) should be (Some(List(4, 4, 4, 5, 5, 5)))
}
test("no coins make 0 change") {
pending
Change.findFewestCoins(0, List(1, 5, 10, 21, 25)) should be (Some(List()))
}
test("error testing for change smaller than the smallest of coins") {
pending
Change.findFewestCoins(3, List(5, 10)) should be (None)
}
test("error if no combination can add up to target") {
pending
Change.findFewestCoins(94, List(5, 10)) should be (None)
}
test("cannot find negative change values") {
pending
Change.findFewestCoins(-5, List(1, 2, 5)) should be (None)
}
}
object Change {
def simpleFind(amount: Int, coins: List[Int], soFar: List[Int]=List()) :Option[List[Int]] = {
if (amount < 0)
None
else if (amount == 0)
Some(soFar)
else {
val maxPossibleCoin = coins.sorted.reverse.find(_ <= amount)
maxPossibleCoin match {
case Some(maxCoin) => simpleFind(amount - maxCoin, coins, maxCoin::soFar)
case None => None
}
}
}
def findCoins(amount: Int, coins: List[Int]) :List[List[Int]] = {
if (amount < 0 || coins.isEmpty)
List[List[Int]]()
else if (amount == 0)
List(List[Int]())
else {
val coin :: rest = coins
findCoins(amount - coin, coins).map(coin :: _) ::: findCoins(amount,rest)
}
}
def canBeSimplified(coins:List[Int]) = {
coins.isEmpty ||
coins.sliding(2, 1).filter{
case first::second::Nil => 2*first > second
}.isEmpty
}
def findFewestCoinsGeneral(amount:Int, coins:List[Int]) = {
val partitions = findCoins(amount, coins)
if (partitions.isEmpty)
None
else
Some(partitions.sortBy(_.length).head)
}
def findFewestCoins(amount: Int, coins: List[Int]) :Option[List[Int]] = {
if (canBeSimplified(coins))
simpleFind(amount, coins) match {
case a:Some[List[Int]] => a
case None => findFewestCoinsGeneral(amount, coins)
}
else
findFewestCoinsGeneral(amount, coins)
}
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments