Avatar of Sendell

Sendell's solution

to Change in the OCaml Track

Published at May 26 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution 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 Base. 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 base:

opam install base

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

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.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

change.ml

open Base

(* Dynamic programming solution with memoization *)
let rec change_m cache target coins =
  Hashtbl.update cache target ~f:(function
    | None -> change cache target coins
    | Some cs -> cs )

and change cache target coins =
  if target = 0 then Some []
  else if target < 0 then None
  else
    List.map coins ~f:(fun c ->
        let new_target = target - c in
        change_m cache new_target coins ;
        Hashtbl.find_exn cache new_target |> Option.map ~f:(fun l -> c :: l) )
    |> List.fold ~init:None ~f:(fun acc cs ->
           match (acc, cs) with
           | Some acc, Some cs ->
               if List.length cs < List.length acc then Some cs else Some acc
           | None, cs -> cs
           | acc, None -> acc )

let make_change ~target ~coins =
  let cache = Hashtbl.create (module Int) in
  change_m cache target coins ;
  Hashtbl.find_exn cache target

Community comments

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

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?