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