Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Stack-based multi-bracket parenthesis matching for ( [ { ) ] }.
Non-bracket chars are skipped (treated as content).
Tests:
() yes
[{()}] yes
({[}]) no (mismatched closer)
"" yes
(( no (unclosed)
()[](){} yes
(a(b)c) yes (a/b/c skipped)
(() no
]) no
5 balanced
Body uses begin/end-wrapped match inside while:
else if c = ')' || c = ']' || c = '}' then begin
match !stack with
| [] -> ok := false
| top :: rest ->
let pair =
(c = ')' && top = '(') ||
(c = ']' && top = '[') ||
(c = '}' && top = '{')
in
if pair then stack := rest else ok := false
end
Tests side-effecting match arms inside while body, ref-of-list as
stack, multi-char pairing dispatch.
174 baseline programs total.
33 lines
807 B
OCaml
33 lines
807 B
OCaml
let bracket_match s =
|
|
let n = String.length s in
|
|
let stack = ref [] in
|
|
let ok = ref true in
|
|
let i = ref 0 in
|
|
while !ok && !i < n do
|
|
let c = s.[!i] in
|
|
if c = '(' || c = '[' || c = '{' then
|
|
stack := c :: !stack
|
|
else if c = ')' || c = ']' || c = '}' then begin
|
|
match !stack with
|
|
| [] -> ok := false
|
|
| top :: rest ->
|
|
let pair =
|
|
(c = ')' && top = '(') ||
|
|
(c = ']' && top = '[') ||
|
|
(c = '}' && top = '{')
|
|
in
|
|
if pair then stack := rest else ok := false
|
|
end;
|
|
i := !i + 1
|
|
done;
|
|
if !ok && !stack = [] then 1 else 0
|
|
|
|
;;
|
|
|
|
let strings = ["()"; "[{()}]"; "({[}])"; ""; "(("; "()[](){}"; "(a(b)c)"; "(()"; "])"] in
|
|
let count = ref 0 in
|
|
List.iter (fun s ->
|
|
count := !count + bracket_match s
|
|
) strings;
|
|
!count
|