ocaml: phase 5.1 max_run.ml baseline (longest consecutive run, 4+1+0 = 5)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Walks list keeping a previous-value reference; increments cur on
match, resets to 1 otherwise. Uses 'Some y when y = x' guard pattern
in match for the prev-value comparison:
let max_run xs =
let max_so_far = ref 0 in
let cur = ref 0 in
let last = ref None in
List.iter (fun x ->
(match !last with
| Some y when y = x -> cur := !cur + 1
| _ -> cur := 1);
last := Some x;
if !cur > !max_so_far then max_so_far := !cur
) xs;
!max_so_far
Three test cases:
[1;1;2;2;2;2;3;3;1;1;1] max run = 4 (the 2's)
[1;2;3;4;5] max run = 1
[] max run = 0
sum = 5
Tests 'when' guard pattern in match arm + Option ref + ref-mutation
sequence inside List.iter closure body.
94 baseline programs total.
This commit is contained in:
@@ -44,6 +44,7 @@
|
||||
"luhn.ml": 2,
|
||||
"mat_mul.ml": 621,
|
||||
"max_path_tree.ml": 11,
|
||||
"max_run.ml": 5,
|
||||
"mod_inverse.ml": 27,
|
||||
"json_pretty.ml": 24,
|
||||
"kadane.ml": 6,
|
||||
|
||||
16
lib/ocaml/baseline/max_run.ml
Normal file
16
lib/ocaml/baseline/max_run.ml
Normal file
@@ -0,0 +1,16 @@
|
||||
let max_run xs =
|
||||
let max_so_far = ref 0 in
|
||||
let cur = ref 0 in
|
||||
let last = ref None in
|
||||
List.iter (fun x ->
|
||||
(match !last with
|
||||
| Some y when y = x -> cur := !cur + 1
|
||||
| _ -> cur := 1);
|
||||
last := Some x;
|
||||
if !cur > !max_so_far then max_so_far := !cur
|
||||
) xs;
|
||||
!max_so_far
|
||||
|
||||
;;
|
||||
|
||||
max_run [1;1;2;2;2;2;3;3;1;1;1] + max_run [1;2;3;4;5] + max_run []
|
||||
Reference in New Issue
Block a user