15 lines
324 B
Plaintext
15 lines
324 B
Plaintext
func balanced (str) {
|
|
|
|
var depth = 0
|
|
str.each { |c|
|
|
if(c=='['){ ++depth }
|
|
elsif(c==']'){ --depth < 0 && return false }
|
|
}
|
|
|
|
return !depth
|
|
}
|
|
|
|
for str [']','[','[[]','][]','[[]]','[[]]]][][]]','x[ y [ [] z ]][ 1 ][]abcd'] {
|
|
printf("%sbalanced\t: %s\n", balanced(str) ? "" : "NOT ", str)
|
|
}
|