Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -1,19 +1,19 @@
import std.stdio, std.random, std.conv, std.range, std.algorithm;
import std.stdio, std.random, std.range, std.algorithm;
bool isBalanced(in string s) pure nothrow {
static bool bal(in string s, in int nb = 0) pure nothrow {
if (nb == 0 && s.empty) return true;
if (s.empty || nb < 0) return false;
if (s[0] == '[') return bal(s[1 .. $], nb + 1);
if (s[0] == ']') return bal(s[1 .. $], nb - 1);
return bal(s[1 .. $], nb); // Ignore char.
bool isBalanced(in string s, in string pars="[]") pure nothrow {
bool bal(in string t, in int nb = 0) pure nothrow {
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, 2)]).array;
immutable s = iota(i * 2).map!(_ => "[]"[uniform(0, $)]).array;
writeln(s.isBalanced ? " OK " : "Bad ", s);
}
}