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,
|
||||
"safe_div.ml": 20,
|
||||
"shuffle.ml": 55,
|
||||
"stable_unique.ml": 46,
|
||||
"subset_sum.ml": 8,
|
||||
"tic_tac_toe.ml": 1,
|
||||
"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])
|
||||
Reference in New Issue
Block a user