Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
19
Task/Balanced-brackets/D/balanced-brackets-3.d
Normal file
19
Task/Balanced-brackets/D/balanced-brackets-3.d
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import std.stdio, std.random, std.range, std.algorithm;
|
||||
|
||||
bool isBalanced(in string s, in char[2] pars="[]") pure nothrow @safe @nogc {
|
||||
bool bal(in string t, in int nb = 0) pure nothrow @safe @nogc {
|
||||
if (!nb && t.empty) return true;
|
||||
if (t.empty || nb < 0) return false;
|
||||
if (t[0] == pars[0]) return bal(t.dropOne, nb + 1);
|
||||
if (t[0] == pars[1]) return bal(t.dropOne, nb - 1);
|
||||
return bal(t.dropOne, nb); // Ignore char.
|
||||
}
|
||||
return bal(s);
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (immutable i; 1 .. 9) {
|
||||
immutable s = iota(i * 2).map!(_ => "[]"[uniform(0, $)]).array;
|
||||
writeln(s.isBalanced ? " OK " : "Bad ", s);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue