From 8ab2f8061507be8561b955281dce66561dd00164 Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 05:04:37 +0000 Subject: [PATCH] ocaml: phase 5.1 daily_temperatures.ml baseline (sum of waits = 10) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Monotonic decreasing stack — for each day i, pop entries from the stack whose temperature is strictly less than today's; their answer is (i - popped_index). temps = [73; 74; 75; 71; 69; 72; 76; 73] answer = [ 1; 1; 4; 2; 1; 1; 0; 0] sum = 10 Complementary to next_greater.ml (iter 256) — same monotonic-stack skeleton but stores the distance to the next greater element rather than its value. Tests `match !stack with | top :: rest when …` pattern with guard inside a while-cont-flag loop. 198 baseline programs total. --- lib/ocaml/baseline/daily_temperatures.ml | 24 ++++++++++++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 10 ++++++++++ 3 files changed, 35 insertions(+) create mode 100644 lib/ocaml/baseline/daily_temperatures.ml diff --git a/lib/ocaml/baseline/daily_temperatures.ml b/lib/ocaml/baseline/daily_temperatures.ml new file mode 100644 index 00000000..8996168b --- /dev/null +++ b/lib/ocaml/baseline/daily_temperatures.ml @@ -0,0 +1,24 @@ +let daily_temperatures temps = + let n = Array.length temps in + let answer = Array.make n 0 in + let stack = ref [] in + for i = 0 to n - 1 do + let cont = ref true in + while !cont do + match !stack with + | top :: rest when temps.(top) < temps.(i) -> + answer.(top) <- i - top; + stack := rest + | _ -> cont := false + done; + stack := i :: !stack + done; + let sum = ref 0 in + for i = 0 to n - 1 do + sum := !sum + answer.(i) + done; + !sum + +;; + +daily_temperatures [| 73; 74; 75; 71; 69; 72; 76; 73 |] diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 7259d399..33932e5d 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -39,6 +39,7 @@ "count_palindromes.ml": 9, "count_subarrays_k.ml": 7, "csv.ml": 10, + "daily_temperatures.ml": 10, "egg_drop.ml": 8, "dijkstra.ml": 7, "dp_word_break.ml": 4, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 943d0d44..defa645b 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 — daily_temperatures.ml baseline (monotonic + decreasing stack of waiting days until warmer; sum over + [73;74;75;71;69;72;76;73] = 10). For each day i, pop all stack + entries whose temperature is strictly less than today's, + recording `i - top` for each. Result vector [1,1,4,2,1,1,0,0] + sums to 10. Complementary to next_greater.ml — same monotonic- + stack skeleton but stores the distance to the next greater + element instead of its value. Tests pattern `match !stack with + | top :: rest when condition -> … | _ -> exit` inside while. + 198 baseline programs total. - 2026-05-11 Phase 5.1 — count_bits.ml baseline (sum of popcount for 0..100 = 319). DP recurrence: popcount(i) = popcount(i/2) + (i mod 2) — drop the low bit and recurse, adding back the bit