From b3ee88e9bbec8fa127d5133c7e9406b18248654d Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 02:44:40 +0000 Subject: [PATCH] ocaml: phase 5.1 kth_two.ml baseline (8th smallest of two sorted = 8) Two-pointer merge advancing the smaller-head pointer k times, without materializing the merged array: while !count < k do let pick_a = if !i = m then false (* a exhausted, take from b *) else if !j = n then true (* b exhausted, take from a *) else a.(!i) <= b.(!j) in if pick_a then ... else ...; count := !count + 1 done For a = [1;3;5;7;9;11;13], b = [2;4;6;8;10;12]: merged order: 1,2,3,4,5,6,7,8,9,10,11,12,13 8th element = 8. Tests nested if/else if/else flowing into a bool, dual-ref two-pointer loop, separate count counter for k-th constraint. 184 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/kth_two.ml | 27 +++++++++++++++++++++++++++ plans/ocaml-on-sx.md | 10 ++++++++++ 3 files changed, 38 insertions(+) create mode 100644 lib/ocaml/baseline/kth_two.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 17bbf814..c2530159 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -102,6 +102,7 @@ "json_pretty.ml": 24, "kadane.ml": 6, "kmp.ml": 5, + "kth_two.ml": 8, "knapsack.ml": 36, "lambda_calc.ml": 7, "lcs.ml": 4, diff --git a/lib/ocaml/baseline/kth_two.ml b/lib/ocaml/baseline/kth_two.ml new file mode 100644 index 00000000..1395f5e5 --- /dev/null +++ b/lib/ocaml/baseline/kth_two.ml @@ -0,0 +1,27 @@ +let kth_two a b k = + let m = Array.length a in + let n = Array.length b in + let i = ref 0 and j = ref 0 and count = ref 0 in + let result = ref 0 in + while !count < k do + let pick_a = + if !i = m then false + else if !j = n then true + else a.(!i) <= b.(!j) + in + if pick_a then begin + result := a.(!i); + i := !i + 1 + end else begin + result := b.(!j); + j := !j + 1 + end; + count := !count + 1 + done; + !result + +;; + +let a = [| 1; 3; 5; 7; 9; 11; 13 |] in +let b = [| 2; 4; 6; 8; 10; 12 |] in +kth_two a b 8 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 5eaec083..fe1ccc6b 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,16 @@ _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-11 Phase 5.1 — kth_two.ml baseline (8th smallest in + merged [1;3;5;7;9;11;13] ∪ [2;4;6;8;10;12] = 8). Two-pointer + merge that advances the smaller-head pointer k times. Pick rule: + pick_a = (j past end) ? true + : (i past end) ? false + : a[i] <= b[j] + Combined order is 1,2,3,4,5,6,7,8,…; 8th element = 8. Tests + nested `if/else if/else` flowing into a bool, dual-ref two- + pointer loop, separate count counter for the k-th constraint. + 184 baseline programs total. - 2026-05-11 Phase 5.1 — permutations_gen.ml baseline (enumerate all 24 permutations of [1;2;3;4], count those with first