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 Nanoquery.Util
 
def gen(N)
	txt = {"[", "]"} * N
	txt = new(Random).shuffle(txt)
	return "".join("", txt)
end
 
def balanced(txt)
	braced = 0
	for ch in txt
		if ch = "["
			braced += 1
		else if ch = "]"
			braced -= 1
		end
 
		if braced < 0
			return false
		end
	end
	return braced = 0
end
 
// unlike Python, the range function is inclusive in Nanoquery
for N in range(1, 10)
	txt = gen(N)
	if balanced(txt)
		println format("%-22s is balanced", txt)
	else
		println format("%-22s is not balanced", txt)
	end
end