ocaml: phase 5.1 paren_depth.ml baseline (max nesting depth, 3+3+1 = 7)
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
One-pass walk tracking current depth and a high-water mark:
let max_depth s =
let d = ref 0 in
let m = ref 0 in
for i = 0 to String.length s - 1 do
if s.[i] = '(' then begin
d := !d + 1;
if !d > !m then m := !d
end
else if s.[i] = ')' then d := !d - 1
done;
!m
Three inputs:
'((1+2)*(3-(4+5)))' 3 (innermost (4+5) at depth 3)
'(((deep)))' 3
'()()()' 1 (no nesting)
sum 7
Tests for-loop char comparison s.[i] = '(' and the high-water-mark
idiom with two refs.
78 baseline programs total.
This commit is contained in:
@@ -52,6 +52,7 @@
|
||||
"mutable_record.ml": 10,
|
||||
"option_match.ml": 5,
|
||||
"palindrome.ml": 4,
|
||||
"paren_depth.ml": 7,
|
||||
"pancake_sort.ml": 910,
|
||||
"pascal.ml": 252,
|
||||
"pi_leibniz.ml": 314,
|
||||
|
||||
15
lib/ocaml/baseline/paren_depth.ml
Normal file
15
lib/ocaml/baseline/paren_depth.ml
Normal file
@@ -0,0 +1,15 @@
|
||||
let max_depth s =
|
||||
let d = ref 0 in
|
||||
let m = ref 0 in
|
||||
for i = 0 to String.length s - 1 do
|
||||
if s.[i] = '(' then begin
|
||||
d := !d + 1;
|
||||
if !d > !m then m := !d
|
||||
end
|
||||
else if s.[i] = ')' then d := !d - 1
|
||||
done;
|
||||
!m
|
||||
|
||||
;;
|
||||
|
||||
max_depth "((1+2)*(3-(4+5)))" + max_depth "(((deep)))" + max_depth "()()()"
|
||||
Reference in New Issue
Block a user