Jump to content

Balanced brackets: Difference between revisions

m
Line 7,970:
[[][[[[[]] is Not Balanced.</pre>
 
Antother version not using libraries. The generator works as intended (more difficult to do than the checking)
<lang vb>
option explicit
Line 7,978:
if bal then s="[":cnt=1 else s="":cnt=0
for i=1 to 20
if (rnd>0.7) then r=not r
if not r then
s=s+"[" :cnt=cnt+1
else
if not bal or (bal and cnt>0) then s=s+"]":cnt=cnt-1
end if
next
if bal and cnt<>0 then s=s&string(cnt,"]")
if not bal and cnt=0 then s=s&"]"
do_brackets=s
end function
Line 7,993:
dim s1,i,cnt: cnt=0
for i=1 to len(s)
s1=mid(s,i,1)
if s1="[" then cnt=cnt+1
if s1="]" then cnt=cnt-1
if cnt<0 then isbalanced=false:exit function
next
isbalanced=(cnt=0)
end function
Line 8,007:
for i=1 to 10
bal=(rnd>.5)
s=do_brackets(bal)
bb=isbalanced(s)
wscript.echo iif (bal,"","un")& "balanced string " &vbtab _
& s & vbtab & " Checks as " & iif(bb,"","un")&"balanced"
next
</lang>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.