34 lines
524 B
Plaintext
34 lines
524 B
Plaintext
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
|