ocaml: phase 5.1 stable_unique.ml baseline (Hashtbl dedupe preserving order, 8+38 = 46)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
Walk input with Hashtbl.mem + Hashtbl.add seen x () (unit-payload
turns the table into a set); on first occurrence cons to the result
list; reverse at the end:
let stable_unique xs =
let seen = Hashtbl.create 8 in
let result = ref [] in
List.iter (fun x ->
if not (Hashtbl.mem seen x) then begin
Hashtbl.add seen x ();
result := x :: !result
end
) xs;
List.rev !result
For [3;1;4;1;5;9;2;6;5;3;5;8;9]:
result = [3;1;4;5;9;2;6;8] (input order, dupes dropped)
length = 8, sum = 38 total = 46
Tests Hashtbl as a set abstraction (unit-payload), the rev-build
idiom, and 'not (Hashtbl.mem seen x)' membership negation.
82 baseline programs total.
This commit is contained in:
@@ -70,6 +70,7 @@
|
|||||||
"run_length.ml": 11,
|
"run_length.ml": 11,
|
||||||
"safe_div.ml": 20,
|
"safe_div.ml": 20,
|
||||||
"shuffle.ml": 55,
|
"shuffle.ml": 55,
|
||||||
|
"stable_unique.ml": 46,
|
||||||
"subset_sum.ml": 8,
|
"subset_sum.ml": 8,
|
||||||
"tic_tac_toe.ml": 1,
|
"tic_tac_toe.ml": 1,
|
||||||
"word_freq.ml": 8,
|
"word_freq.ml": 8,
|
||||||
|
|||||||
15
lib/ocaml/baseline/stable_unique.ml
Normal file
15
lib/ocaml/baseline/stable_unique.ml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
let stable_unique xs =
|
||||||
|
let seen = Hashtbl.create 8 in
|
||||||
|
let result = ref [] in
|
||||||
|
List.iter (fun x ->
|
||||||
|
if not (Hashtbl.mem seen x) then begin
|
||||||
|
Hashtbl.add seen x ();
|
||||||
|
result := x :: !result
|
||||||
|
end
|
||||||
|
) xs;
|
||||||
|
List.rev !result
|
||||||
|
|
||||||
|
;;
|
||||||
|
|
||||||
|
List.length (stable_unique [3;1;4;1;5;9;2;6;5;3;5;8;9])
|
||||||
|
+ List.fold_left (+) 0 (stable_unique [3;1;4;1;5;9;2;6;5;3;5;8;9])
|
||||||
@@ -407,6 +407,14 @@ _Newest first._
|
|||||||
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
|
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
|
||||||
'a tree`) with insert + in-order traversal. Tests parametric ADT,
|
'a tree`) with insert + in-order traversal. Tests parametric ADT,
|
||||||
recursive match, List.append, List.fold_left.
|
recursive match, List.append, List.fold_left.
|
||||||
|
- 2026-05-09 Phase 5.1 — stable_unique.ml baseline (Hashtbl-tracked
|
||||||
|
dedupe preserving order, length+sum = 8+38 = 46). Walks input
|
||||||
|
with `Hashtbl.mem` + `Hashtbl.add seen x ()` (unit-payload to use
|
||||||
|
the table as a set); on first occurrence cons to the result list,
|
||||||
|
reverse at the end. For [3;1;4;1;5;9;2;6;5;3;5;8;9] yields
|
||||||
|
[3;1;4;5;9;2;6;8] — 8 elements summing to 38. Tests Hashtbl as
|
||||||
|
a set abstraction (ignoring values), and the rev-build idiom. 82
|
||||||
|
baseline programs total.
|
||||||
- 2026-05-09 Phase 5.1 — run_decode.ml baseline (RLE decode, sum of
|
- 2026-05-09 Phase 5.1 — run_decode.ml baseline (RLE decode, sum of
|
||||||
expansion = 21). Inverse of run_length.ml: takes a list of
|
expansion = 21). Inverse of run_length.ml: takes a list of
|
||||||
`(value, count)` tuples, expands each pair via inner `rep` helper,
|
`(value, count)` tuples, expands each pair via inner `rep` helper,
|
||||||
|
|||||||
Reference in New Issue
Block a user