Avatar of bcc32

bcc32's solution

to Change in the OCaml Track

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

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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?

Getting Started

For installation and learning resources, refer to the exercism help page.

Installation

To work on the exercises, you will need Opam and Core. Consult opam website for instructions on how to install opam for your OS. Once opam is installed open a terminal window and run the following command to install core:

opam install core

To run the tests you will need OUnit. Install it using opam:

opam install ounit

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

make

Interactive Shell

utop is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:

opam install utop

Consult utop for more detail.

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

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.

test.ml

(* Test/exercise version: "1.1.0" *)

open Base
open OUnit2
open Change

let printer = Option.value_map ~default:"None" ~f:(fun xs -> String.concat ~sep:";" (List.map ~f:Int.to_string xs))
let ae exp got _test_ctxt = assert_equal ~printer exp got

let tests = [
   "single coin change" >::
     ae (Some [25]) 
       (make_change ~target:25 ~coins:[1; 5; 10; 25; 100]);
   "multiple coin change" >::
     ae (Some [5; 10]) 
       (make_change ~target:15 ~coins:[1; 5; 10; 25; 100]);
   "change with Lilliputian Coins" >::
     ae (Some [4; 4; 15]) 
       (make_change ~target:23 ~coins:[1; 4; 15; 20; 50]);
   "change with Lower Elbonia Coins" >::
     ae (Some [21; 21; 21]) 
       (make_change ~target:63 ~coins:[1; 5; 10; 21; 25]);
   "large target values" >::
     ae (Some [2; 2; 5; 20; 20; 50; 100; 100; 100; 100; 100; 100; 100; 100; 100]) 
       (make_change ~target:999 ~coins:[1; 2; 5; 10; 20; 50; 100]);
   "possible change without unit coins available" >::
     ae (Some [2; 2; 2; 5; 10]) 
       (make_change ~target:21 ~coins:[2; 5; 10; 20; 50]);
   "another possible change without unit coins available" >::
     ae (Some [4; 4; 4; 5; 5; 5]) 
       (make_change ~target:27 ~coins:[4; 5]);
   "no coins make 0 change" >::
     ae (Some []) 
       (make_change ~target:0 ~coins:[1; 5; 10; 21; 25]);
   "error testing for change smaller than the smallest of coins" >::
     ae None 
       (make_change ~target:3 ~coins:[5; 10]);
   "error if no combination can add up to target" >::
     ae None 
       (make_change ~target:94 ~coins:[5; 10]);
   "cannot find negative change values" >::
     ae None 
       (make_change ~target:(-5) ~coins:[1; 2; 5]);
]

let () =
  run_test_tt_main ("change tests" >::: tests)

change.ml

open Core

module Change = struct
  module T = struct
    type t =
      { length : int
      ; coins  : int list
      }
    [@@deriving sexp]
    ;;
    let compare a b = Int.compare a.length b.length
  end
  include T
  include Comparable.Make(T)

  let empty = { length = 0; coins = [] }

  let add { length; coins } ~coin =
    { length = length + 1
    ; coins  = coin :: coins
    }
  ;;

  let coins t = List.sort t.coins ~cmp:Int.ascending
end

let make_change ~target ~coins =
  if target < 0
  then None
  else begin
    let shortest = Array.create None ~len:(target + 1) in
    let update_shortest target change =
      let current = shortest.(target) in
      let new_solution = Option.merge current change ~f:Change.min in
      shortest.(target) <- new_solution
    in
    shortest.(0) <- Some Change.empty;
    List.iter coins ~f:(fun coin ->
      for i = 0 to target - coin do
        let new_target = i + coin in
        shortest.(i)
        |> Option.map ~f:(Change.add ~coin)
        |> update_shortest new_target
      done);
    shortest.(target)
    |> Option.map ~f:Change.coins
  end
;;

change.mli

(* If it is impossible to make the target with the given coins, then return None *)
val make_change : target: int -> coins: int list -> int list option

Community comments

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

Good optimization to avoid calls to List.length.

Avatar of bcc32

Thanks. It seemed possibly excessive but I felt that I might as well make a module, seeing as I could use Comparable as well.

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?