Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Balanced-brackets/Ecstasy/balanced-brackets.ecstasy
Normal file
37
Task/Balanced-brackets/Ecstasy/balanced-brackets.ecstasy
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
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.print($"{test}{' ' * (longest-test.size)} {balanced(test) ? "OK" : "NOT OK"}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue