update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
|
|
@ -1,10 +1,8 @@
|
|||
import std.stdio, std.algorithm, std.random;
|
||||
import std.stdio, std.algorithm, std.random, std.range;
|
||||
|
||||
void main() {
|
||||
foreach (i; 1 .. 9) {
|
||||
string s;
|
||||
foreach (_; 0 .. i * 2)
|
||||
s ~= "[]"[uniform(0, 2)];
|
||||
foreach (immutable i; 1 .. 9) {
|
||||
immutable s = iota(i * 2).map!(_ => "[]"[uniform(0, 2)]).array;
|
||||
writeln(s.balancedParens('[', ']') ? " OK: " : "bad: ", s);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,23 @@
|
|||
import std.stdio, std.random;
|
||||
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() {
|
||||
NEXT_STR:
|
||||
foreach (j; 1 .. 9) {
|
||||
string s;
|
||||
foreach (_; 0 .. j * 2)
|
||||
s ~= "[]"[uniform(0, 2)];
|
||||
|
||||
int balance;
|
||||
foreach (i, c; s) {
|
||||
balance += (c == '[') ? 1 : ((c == ']') ? -1 : 0);
|
||||
if (balance < 0 || balance >= s.length - i) {
|
||||
writefln("BAD: %s (%s)", s, balance < 0 ? "-" : "+");
|
||||
continue NEXT_STR;
|
||||
}
|
||||
}
|
||||
writefln(" OK: %s (=)", s);
|
||||
foreach (immutable i; 1 .. 9) {
|
||||
immutable s = iota(i * 2).map!(_ => "[]"[uniform(0, 2)]).array;
|
||||
writeln(s.isBalanced ? " OK " : "Bad ", s);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,19 @@
|
|||
import std.stdio, std.random, std.conv, std.range, std.algorithm;
|
||||
|
||||
bool isBalanced(in char[] s) pure nothrow {
|
||||
static bool bal(in char[] s, in int nb=0) pure nothrow {
|
||||
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
|
||||
return bal(s[1 .. $], nb); // Ignore char.
|
||||
}
|
||||
return bal(s);
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (i; 1 .. 9) {
|
||||
auto sr = iota(i * 2).map!(_ => ['[',']'][uniform(0, 2)])();
|
||||
auto s = sr.array();
|
||||
writefln("%5s: %s", s.isBalanced().text(), s);
|
||||
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