Jump to content

Balanced brackets: Difference between revisions

m
Line 2,389:
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module BalancedBrackets {
Boolean balanced(String text) {
{
Boolean balanced(String text)
{
Int depth = 0;
for (Char ch : text) {
switch (ch, depth) {
switchcase (ch'[', depth_):
{++depth;
case ('[', _):break;
case (']', ++depth;0):
return breakFalse;
case (']', 0_):
return False--depth;
case (']', _):break;
--depth;
break;
}
}
return depth==0;
}
return --depth==0;
{}
 
@Inject Console console;
void run() {
{
String[] tests =
[
Line 2,426 ⟶ 2,421:
];
Int longest = tests.map(s -> s.size).reduce(0, (max, len) -> max.maxOf(len));
for (String test : tests) {
{
console.print($"{test}{' ' * (longest-test.size)} {balanced(test) ? "OK" : "NOT OK"}");
}
}
}
}
</syntaxhighlight>
 
162

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.