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



import system'routines;
import extensions;
import extensions'text;
 
randomBrackets(len)
{
    if (0 == len)
    { 
        ^emptyString 
    }
    else
    {
        var brackets := 
            Array.allocate(len).populate:(i => $91) 
            + 
            Array.allocate(len).populate:(i => $93);
 
        brackets := brackets.randomize(len * 2);
 
        ^ brackets.summarize(new StringWriter()).toString()
    }
}
 
extension op
{
    get isBalanced()
    {
        var counter := new Integer(0);
 
        self.seekEach:(ch => counter.append((ch==$91).iif(1,-1)) < 0);
 
        ^ (0 == counter)
    }
}
 
public program()
{
    for(int len := 0, len < 9, len += 1)
    {
        var str := randomBrackets(len);
 
        console.printLine("""",str,"""",str.isBalanced ? " is balanced" : " is not balanced")
    };
 
    console.readChar()
}