tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,18 @@
import std.stdio;
T[][] ncsub(T)(in T[] seq, in int s=0) pure nothrow {
if (seq.length) {
T[][] aux;
foreach (ys; ncsub(seq[1..$], s + !(s % 2)))
aux ~= seq[0] ~ ys;
return aux ~ ncsub(seq[1..$], s + s % 2);
} else
return new T[][](s >= 3, 0);
}
void main() {
writeln(ncsub([1, 2, 3]));
writeln(ncsub([1, 2, 3, 4]));
foreach (nc; ncsub([1, 2, 3, 4, 5]))
writeln(nc);
}

View file

@ -0,0 +1,43 @@
struct Ncsub(T) {
T[] seq;
int opApply(int delegate(ref int[]) dg) const {
immutable int n = seq.length;
int result;
auto S = new int[n];
FOR_I:
foreach (i; 1 .. 1 << seq.length) {
int len_S;
bool nc = false;
foreach (j; 0 .. seq.length + 1) {
immutable int k = i >> j;
if (k == 0) {
if (nc) {
auto auxS = S[0 .. len_S];
result = dg(auxS);
if (result)
break FOR_I;
}
break;
} else if (k % 2) {
S[len_S] = seq[j];
len_S++;
} else if (len_S)
nc = true;
}
}
return result;
}
}
void main() {
import std.array, std.range;
//assert(iota(24).array().Ncsub!int().walkLength() == 16_776_915);
auto r = array(iota(24));
int counter;
foreach (s; Ncsub!int(r))
counter++;
assert(counter == 16_776_915);
}