27 lines
792 B
Plaintext
27 lines
792 B
Plaintext
module Balanced_brackets{
|
|
// Generate a string with N opening brackets [ and with N closing brackets ], in some arbitrary order.
|
|
function randomBrackets(max as long) {
|
|
if max<1 then max=1
|
|
def putbracket()=mid$("[[[[]]",random(1,6),1)
|
|
dim a(random(1, max)) as string<<putbracket()
|
|
=a()#str$("")
|
|
}
|
|
// Determine whether the generated string is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.
|
|
function check(s as string) {
|
|
long i, level
|
|
for i=1 to len(s)
|
|
if mid$(s,i,1)="[" then level++ else level--
|
|
if level<0 then exit for
|
|
next
|
|
=level=0
|
|
}
|
|
string k
|
|
boolean m
|
|
do
|
|
k=randomBrackets(10)
|
|
m=check(k)
|
|
print k;@(12);": ";if$(m->"OK", "NOT OK")
|
|
until m
|
|
}
|
|
Balanced_brackets
|