Balanced brackets: Difference between revisions

Add Ecstasy example
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(Add Ecstasy example)
Line 2,385:
❌ "[[][]]]["
</syntaxhighlight>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module BalancedBrackets
{
Boolean balanced(String text)
{
Int depth = 0;
for (Char ch : text)
{
switch (ch, depth)
{
case ('[', _):
++depth;
break;
case (']', 0):
return False;
case (']', _):
--depth;
break;
}
}
return depth==0;
}
 
@Inject Console console;
void run()
{
String[] tests =
[
"[]",
"[][]",
"[]][[]",
"[[[]][]]",
"][[[[]][]]",
"[[[]][[]][]]",
"]][[]][[[[][]]",
"[[]]]][]][[][[[]",
];
Int longest = tests.map(s -> s.size).reduce(0, (max, len) -> max.maxOf(len));
for (String test : tests)
{
console.println($"{test}{' ' * (longest-test.size)} {balanced(test) ? "OK" : "NOT OK"}");
}
}
}
</syntaxhighlight>
 
Resulting output:
<syntaxhighlight>
[] OK
[][] OK
[]][[] NOT OK
[[[]][]] OK
][[[[]][]] NOT OK
[[[]][[]][]] OK
]][[]][[[[][]] NOT OK
[[]]]][]][[][[[] NOT OK
</syntaxhighlight>
 
162

edits