ocaml: phase 5.1 group_consec.ml baseline (group consecutive equals, 5*10+3 = 53)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 34s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 34s
Inner 'collect cur acc tail' walks the tail while head matches 'cur', accumulating into 'acc'. Returns (rev acc, remaining) on first mismatch. Outer 'group' recurses on the remaining list. group [1;1;2;2;2;3;1;1;4] = [[1;1]; [2;2;2]; [3]; [1;1]; [4]] List.length groups = 5 List.length (gs.(1)) = 3 5 * 10 + 3 = 53 Tests nested recursion (inner aux + outer recursion), tuple destructuring 'let (g, tail) = ...' inside the outer match arm, and List.nth. 69 baseline programs total.
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
"frequency.ml": 5,
|
||||
"gcd_lcm.ml": 60,
|
||||
"grep_count.ml": 3,
|
||||
"group_consec.ml": 53,
|
||||
"hailstone.ml": 111,
|
||||
"hanoi.ml": 1023,
|
||||
"fizzbuzz.ml": 57,
|
||||
|
||||
18
lib/ocaml/baseline/group_consec.ml
Normal file
18
lib/ocaml/baseline/group_consec.ml
Normal file
@@ -0,0 +1,18 @@
|
||||
let rec group xs =
|
||||
match xs with
|
||||
| [] -> []
|
||||
| x :: rest ->
|
||||
let rec collect cur acc tail =
|
||||
match tail with
|
||||
| [] -> (List.rev acc, [])
|
||||
| y :: ys ->
|
||||
if y = cur then collect cur (y :: acc) ys
|
||||
else (List.rev acc, y :: ys)
|
||||
in
|
||||
let (g, tail) = collect x [x] rest in
|
||||
g :: group tail
|
||||
|
||||
;;
|
||||
|
||||
let gs = group [1;1;2;2;2;3;1;1;4] in
|
||||
List.length gs * 10 + List.length (List.nth gs 1)
|
||||
Reference in New Issue
Block a user