Avatar of pismute

pismute's solution

to Change in the Scala Track

Published at Jul 13 2018 · 2 comments
Instructions
Test suite
Solution

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.

For example

  • An input of 15 with [1, 5, 10, 25, 100] should return one nickel (5) and one dime (10) or [0, 1, 1, 0, 0]
  • An input of 40 with [1, 5, 10, 25, 100] should return one nickel (5) and one dime (10) and one quarter (25) or [0, 1, 1, 1, 0]

Edge cases

  • Does your algorithm work for any given set of coins?
  • Can you ask for negative change?
  • Can you ask for a change value smaller than the smallest coin value?

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.

For more detailed info about the Scala track see the help page.

Source

Software Craftsmanship - Coin Change Kata https://web.archive.org/web/20130115115225/http://craftsmanship.sv.cmu.edu:80/exercises/coin-change-kata

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ChangeTest.scala

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)
  }
}
import scala.annotation.tailrec

object Change {
  def validMoney(money: Int, minCoin: Int): Option[Int] =
    if(money == 0) Option(money)
    else if(money < minCoin || money < 0) None
    else Option(money)

  def fewestCoins(money: Int, coins: Seq[Int]): Option[Seq[Int]] = {
    @tailrec
    def loop(tries: Seq[(Int, Seq[Int], Seq[Int])], // Seq((money, coins, changes))
        acc: Option[Seq[Int]]): Option[Seq[Int]] =
      tries match {
        case Seq() => acc
        case Seq((0, _, changes), xs @ _*) =>
          loop(xs, acc.filter(_.size < changes.size).orElse(Option(changes)))
        case Seq((_, Seq(), _), xs @ _*) => loop(xs, acc)
        case Seq((m, coins @ Seq(c, cs @ _*), changes), xs @ _*) if acc.forall(_.size > changes.size) =>
          val next = (m, cs, changes) +: xs
          val appended =
            if(m < c) next
            else (m-c, coins, c +: changes) +: next

          loop(appended, acc)
        case Seq(_, xs @ _*) => loop(xs, acc)
      }

    loop(Seq((money, coins, Seq())), None)
  }

  def findFewestCoins(money: Int, coins: Seq[Int]): Option[Seq[Int]] =
    for {
      m <- validMoney(money, coins.min)
      changes <- fewestCoins(money, coins.sortBy(-_))
    } yield changes

}

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of pismute

Thank you so much @abo64.

I have spent much time to figure out what you are saying, even though I have read your solution. I was stuck--). Anyway, You let me be in the sunshine.

Avatar of abo64

you are welcome. :) if you want you can work a bit on readability, this match block is quite terse.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?