Balanced brackets: Difference between revisions
Content added Content deleted
Line 7,970: | Line 7,970: | ||
[[][[[[[]] is Not Balanced.</pre> |
[[][[[[[]] is Not Balanced.</pre> |
||
Antother version not using libraries. The generator works as intended |
Antother version not using libraries. The generator works as intended (more difficult to do than the checking) |
||
<lang vb> |
<lang vb> |
||
option explicit |
option explicit |
||
Line 7,978: | Line 7,978: | ||
if bal then s="[":cnt=1 else s="":cnt=0 |
if bal then s="[":cnt=1 else s="":cnt=0 |
||
for i=1 to 20 |
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 |
next |
||
if bal and cnt<>0 then s=s&string(cnt,"]") |
|||
if not bal and cnt=0 then s=s&"]" |
|||
do_brackets=s |
do_brackets=s |
||
end function |
end function |
||
Line 7,993: | Line 7,993: | ||
dim s1,i,cnt: cnt=0 |
dim s1,i,cnt: cnt=0 |
||
for i=1 to len(s) |
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 |
if cnt<0 then isbalanced=false:exit function |
||
next |
|||
isbalanced=(cnt=0) |
isbalanced=(cnt=0) |
||
end function |
end function |
||
Line 8,007: | Line 8,007: | ||
for i=1 to 10 |
for i=1 to 10 |
||
bal=(rnd>.5) |
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 |
next |
||
</lang> |
</lang> |