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



string.prototype.balanced? = {
  brackets = []
  balanced = true
 
  my.dice.each_while { c |
    true? c == "["
      { brackets << c }
      { true? c == "]"
        { last = brackets.pop
          false? last == "["
          { balanced = false }
        }
      }
 
    balanced
  }
 
  true? brackets.empty?
    { balanced }
    { false }
}
 
generate_brackets = { n | (n.of("[") + n.of("]")).shuffle.join }
 
1.to 10 { n |
  test = generate_brackets n
 
  true? test.balanced?
    { p "#{test} is balanced" }
    { p "#{test} is not balanced" }
}