26 lines
532 B
Plaintext
26 lines
532 B
Plaintext
go1 ?=>
|
|
tests(Tests),
|
|
member(Test,Tests),
|
|
printf("%s: ", Test),
|
|
( balanced_brackets(Test) ->
|
|
println("OK")
|
|
;
|
|
println("NOT OK")
|
|
),
|
|
fail,
|
|
nl.
|
|
go1 => true.
|
|
|
|
% Check if a string of [] is balanced
|
|
balanced_brackets(B) =>
|
|
C = 0,
|
|
foreach(I in 1..B.length, C >= 0)
|
|
C:= C + cond(B[I] = '[', 1, -1)
|
|
end,
|
|
C == 0.
|
|
|
|
tests(["","[]", "[][]", "[[][]]", "][",
|
|
"][][", "[]][[]", "[][][][][][][][][][]",
|
|
"[[[[[[[]]]]]]]", "[[[[[[[]]]]]]",
|
|
"[][[]][]","[[][]][]", "[][][[]][]"]).
|