From 2129e04bfd3bc4176220ded9ba09c9da75094ad3 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 16:28:15 +0000 Subject: [PATCH] ocaml: phase 5.1 count_change.ml baseline (ways to make 50c from [1;2;5;10;25] = 406) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Companion to coin_change.ml (min coins). Counts distinct multisets via the unbounded-knapsack DP: let count_ways coins target = let dp = Array.make (target + 1) 0 in dp.(0) <- 1; List.iter (fun c -> for i = c to target do dp.(i) <- dp.(i) + dp.(i - c) done ) coins; dp.(target) Outer loop over coins, inner DP relaxes dp.(i) += dp.(i - c). The order matters — coin in outer, amount in inner — to count multisets rather than ordered sequences. count_ways [1; 2; 5; 10; 25] 50 = 406. 79 baseline programs total. --- lib/ocaml/baseline/count_change.ml | 13 +++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 7 +++++++ 3 files changed, 21 insertions(+) create mode 100644 lib/ocaml/baseline/count_change.ml diff --git a/lib/ocaml/baseline/count_change.ml b/lib/ocaml/baseline/count_change.ml new file mode 100644 index 00000000..f9f5796d --- /dev/null +++ b/lib/ocaml/baseline/count_change.ml @@ -0,0 +1,13 @@ +let count_ways coins target = + let dp = Array.make (target + 1) 0 in + dp.(0) <- 1; + List.iter (fun c -> + for i = c to target do + dp.(i) <- dp.(i) + dp.(i - c) + done + ) coins; + dp.(target) + +;; + +count_ways [1; 2; 5; 10; 25] 50 diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 469e8e6c..99345527 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -18,6 +18,7 @@ "calc.ml": 13, "closures.ml": 315, "coin_change.ml": 6, + "count_change.ml": 406, "csv.ml": 10, "exception_handle.ml": 4, "exception_user.ml": 26, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index a02864ac..4c4d3507 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,13 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-09 Phase 5.1 — count_change.ml baseline (number of ways to + make 50c from [1;2;5;10;25] = 406). Companion to coin_change.ml + (min coins): instead of minimising, this counts distinct + multisets. Outer loop over coins, inner DP `dp.(i) += dp.(i - c)` + (the standard "unbounded knapsack" count). Tests Array.make + + arr.(i) accumulation through nested List.iter / for. 79 baseline + programs total. - 2026-05-09 Phase 5.1 — paren_depth.ml baseline (max paren nesting depth, 3+3+1 = 7). One-pass walk tracking current depth and a high-water mark. Tests three inputs: