Avatar of emengd

emengd's solution

to Change in the OCaml Track

Published at Feb 21 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)
open Base

let make_change ~target ~coins =
  if target < 0 then
    None
  else if target = 0 then
    Some []
  else
    let coins_array = Array.of_list coins in
    let rec loop target index best length coins =
      if index < 0 then
        best
      else if Option.value_map best ~default:false ~f:(fun (l, _) -> length >= l) then
        best
      else
        let c = coins_array.(index) in
        let new_target = target - c in
        if new_target < 0 then
          loop target (index - 1) best length coins
        else
          let new_length = length + 1 in
          let new_coins = c :: coins in
          if new_target = 0 then
            Some (new_length, new_coins)
          else
            let best = loop new_target index best new_length new_coins in
            loop target (index - 1) best length coins
    in
    Option.map ~f:snd (loop target (Array.length coins_array - 1) None 0 [])

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?