Task:

 Generate a string with   N   opening brackets   [   and with   N   closing brackets   ],   in some arbitrary order. 
 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.


Examples

   (empty)      OK
   []           OK   
   [][]         OK   
   [[][]]       OK 
   ][         NOT OK
   ][][       NOT OK
   []][[]     NOT OK



PROC Generate(BYTE size CHAR ARRAY s)
  BYTE i,half
 
  s(0)=size
  half=size RSH 1
 
  FOR i=1 TO half 
  DO s(i)='[ OD
 
  FOR i=half+1 TO size
  DO s(i)='] OD
RETURN
 
PROC Shuffle(CHAR ARRAY s)
  BYTE i,j,k,n,len,tmp
 
  len=s(0)
  n=Rand(len+1)
  FOR k=1 TO n
  DO
    i=Rand(len)+1
    j=Rand(len)+1
    tmp=s(i)
    s(i)=s(j)
    s(j)=tmp
  OD
RETURN
 
BYTE FUNC Balanced(CHAR ARRAY s)
  INT i,lev
 
  lev=0
  FOR i=1 TO s(0)
  DO
    IF s(i)='[ THEN
      lev==+1
    ELSE
      lev==-1
    FI
 
    IF lev<0 THEN
      RETURN (0)
    FI
  OD
 
  IF lev#0 THEN
    RETURN (0)
  FI
RETURN (1)
 
PROC Main()
  CHAR ARRAY s(20)
  BYTE i,b
 
  FOR i=0 TO 20 STEP 2
  DO
    Generate(i,s)
    Shuffle(s)
    b=Balanced(s)
 
    IF s(0)=0 THEN
      Print("(empty)")
    ELSE
      Print(s)
    FI
    Print(" is ")
    IF b=0 THEN
      Print("not ")
    FI
    PrintE("balanced")
  OD
RETURN