Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
23
Task/Balanced-brackets/D/balanced-brackets-2.d
Normal file
23
Task/Balanced-brackets/D/balanced-brackets-2.d
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import std.stdio, std.random, std.range, std.algorithm;
|
||||
|
||||
bool isBalanced(in string txt) pure nothrow {
|
||||
auto count = 0;
|
||||
|
||||
foreach (immutable c; txt) {
|
||||
if (c == ']') {
|
||||
count--;
|
||||
if (count < 0)
|
||||
return false;
|
||||
} else if (c == '[')
|
||||
count++;
|
||||
}
|
||||
|
||||
return count == 0;
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (immutable i; 1 .. 9) {
|
||||
immutable s = iota(i * 2).map!(_ => "[]"[uniform(0, 2)]).array;
|
||||
writeln(s.isBalanced ? " OK " : "Bad ", s);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue