Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
8
Task/Balanced-brackets/D/balanced-brackets-1.d
Normal file
8
Task/Balanced-brackets/D/balanced-brackets-1.d
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import std.stdio, std.algorithm, std.random, std.range;
|
||||
|
||||
void main() {
|
||||
foreach (immutable i; 1 .. 9) {
|
||||
immutable s = iota(i * 2).map!(_ => "[]"[uniform(0, 2)]).array;
|
||||
writeln(s.balancedParens('[', ']') ? " OK: " : "bad: ", s);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
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