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,29 @@
import std.stdio, std.algorithm, std.array;
dchar[][] consolidate(dchar[][] sets) {
foreach (set; sets)
set.sort;
foreach (i, ref si; sets[0 .. $ - 1]) {
if (si.empty)
continue;
foreach (ref sj; sets[i + 1 .. $])
if (!sj.empty && !si.setIntersection(sj).empty) {
sj = si.setUnion(sj).uniq.array;
si = null;
}
}
return sets.filter!"!a.empty".array;
}
void main() {
[['A', 'B'], ['C','D']].consolidate.writeln;
[['A','B'], ['B','D']].consolidate.writeln;
[['A','B'], ['C','D'], ['D','B']].consolidate.writeln;
[['H','I','K'], ['A','B'], ['C','D'],
['D','B'], ['F','G','H']].consolidate.writeln;
}

View file

@ -0,0 +1,32 @@
import std.stdio, std.algorithm, std.array;
dchar[][] consolidate(dchar[][] sets) {
foreach (set; sets)
set.sort;
dchar[][] consolidateR(dchar[][] s) {
if (s.length < 2)
return s;
auto r = [s[0]];
foreach (x; consolidateR(s[1 .. $])) {
if (!r[0].setIntersection(x).empty) {
r[0] = r[0].setUnion(x).uniq.array;
} else
r ~= x;
}
return r;
}
return consolidateR(sets);
}
void main() {
[['A', 'B'], ['C','D']].consolidate.writeln;
[['A','B'], ['B','D']].consolidate.writeln;
[['A','B'], ['C','D'], ['D','B']].consolidate.writeln;
[['H','I','K'], ['A','B'], ['C','D'],
['D','B'], ['F','G','H']].consolidate.writeln;
}