RosettaCodeData/Task/Balanced-brackets/Brat/balanced-brackets.brat
2023-07-01 13:44:08 -04:00

31 lines
547 B
Text

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" }
}