From 17a7a91d73bfa1e19c5dd939a6f9fc72e8c8d359 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 18:39:46 +0000 Subject: [PATCH] ocaml: phase 5.1 merge_intervals.ml baseline (LeetCode #56, total length 9+3 = 12) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sort intervals by start, then sweep maintaining a current (cs, ce) window — extend ce if next start <= ce, else push current and start fresh: let merge_intervals xs = let sorted = List.sort (fun (a, _) (b, _) -> a - b) xs in let rec aux acc cur xs = match xs with | [] -> List.rev (cur :: acc) | (s, e) :: rest -> let (cs, ce) = cur in if s <= ce then aux acc (cs, max e ce) rest else aux (cur :: acc) (s, e) rest in match sorted with | [] -> [] | h :: rest -> aux [] h rest [(1,3);(2,6);(8,10);(15,18);(5,9)] -> [(1,10); (15,18)] total length = 9 + 3 = 12 Tests List.sort with custom comparator using tuple patterns, plus tuple destructuring in lambda + let-tuple from accumulator + match arms. 92 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/merge_intervals.ml | 21 +++++++++++++++++++++ plans/ocaml-on-sx.md | 8 ++++++++ 3 files changed, 30 insertions(+) create mode 100644 lib/ocaml/baseline/merge_intervals.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 777c5e41..8f60dd64 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -51,6 +51,7 @@ "levenshtein.ml": 11, "memo_fib.ml": 75025, "mortgage.ml": 1073, + "merge_intervals.ml": 12, "merge_sort.ml": 44, "merge_two.ml": 441, "module_use.ml": 3, diff --git a/lib/ocaml/baseline/merge_intervals.ml b/lib/ocaml/baseline/merge_intervals.ml new file mode 100644 index 00000000..00bd9212 --- /dev/null +++ b/lib/ocaml/baseline/merge_intervals.ml @@ -0,0 +1,21 @@ +let merge_intervals xs = + let sorted = List.sort (fun (a, _) (b, _) -> a - b) xs in + let rec aux acc cur xs = + match xs with + | [] -> List.rev (cur :: acc) + | (s, e) :: rest -> + let (cs, ce) = cur in + if s <= ce then + let new_e = if e > ce then e else ce in + aux acc (cs, new_e) rest + else + aux (cur :: acc) (s, e) rest + in + match sorted with + | [] -> [] + | h :: rest -> aux [] h rest + +;; + +let m = merge_intervals [(1, 3); (2, 6); (8, 10); (15, 18); (5, 9)] in +List.fold_left (fun acc (s, e) -> acc + e - s) 0 m diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 6ef1d29a..3a66d964 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,14 @@ _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 — merge_intervals.ml baseline (LeetCode #56, + total length 9 + 3 = 12). Sort by start, then sweep maintaining a + current `(cs, ce)` window — extend `ce` if next start ≤ ce, else + push current and start new. `[(1,3);(2,6);(8,10);(15,18);(5,9)]` + merges to `[(1,10);(15,18)]`, total length 9+3 = 12. Tests + List.sort with custom cmp + tuple destructuring everywhere + (closure lambda with tuple-pattern, let-tuple from accumulator, + match arms). 92 baseline programs total. - 2026-05-09 Phase 5.1 — hamming.ml baseline (Hamming distance, 3 + 2 + (-1) = 4). Counts position-wise differences in equal-length strings; returns -1 sentinel for length mismatch.