Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Tracks two bool refs (inc, dec). For each pair of consecutive
elements: if h < prev clear inc, if h > prev clear dec. Returns
inc OR dec at the end:
let is_monotonic xs =
match xs with
| [] -> true
| [_] -> true
| _ ->
let inc = ref true in
let dec = ref true in
let rec walk prev rest = ... in
(match xs with h :: t -> walk h t | [] -> ());
!inc || !dec
Five test cases:
[1;2;3;4] inc only true
[4;3;2;1] dec only true
[1;2;1] neither false
[5;5;5] both (constant) true
[] empty true (vacuous)
sum = 4
98 baseline programs total.
26 lines
598 B
OCaml
26 lines
598 B
OCaml
let is_monotonic xs =
|
|
match xs with
|
|
| [] -> true
|
|
| [_] -> true
|
|
| _ ->
|
|
let inc = ref true in
|
|
let dec = ref true in
|
|
let rec walk prev rest =
|
|
match rest with
|
|
| [] -> ()
|
|
| h :: t ->
|
|
if h < prev then inc := false;
|
|
if h > prev then dec := false;
|
|
walk h t
|
|
in
|
|
(match xs with h :: t -> walk h t | [] -> ());
|
|
!inc || !dec
|
|
|
|
;;
|
|
|
|
(if is_monotonic [1;2;3;4] then 1 else 0) +
|
|
(if is_monotonic [4;3;2;1] then 1 else 0) +
|
|
(if is_monotonic [1;2;1] then 1 else 0) +
|
|
(if is_monotonic [5;5;5] then 1 else 0) +
|
|
(if is_monotonic [] then 1 else 0)
|