tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
34
Task/Ordered-Partitions/D/ordered-partitions.d
Normal file
34
Task/Ordered-Partitions/D/ordered-partitions.d
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import std.stdio, std.algorithm, std.range, std.array, std.conv;
|
||||
// http://rosettacode.org/wiki/Combinations#D
|
||||
import combinations4: Comb;
|
||||
|
||||
alias int[] iRNG;
|
||||
|
||||
iRNG setDiff(iRNG s, iRNG c) {
|
||||
return setDifference(s, c).array();
|
||||
}
|
||||
|
||||
iRNG[][] orderPart(iRNG blockSize...) {
|
||||
iRNG sum = iota(1, 1 + blockSize.reduce!q{a + b}()).array();
|
||||
|
||||
iRNG[][] p(iRNG s, in iRNG b) {
|
||||
if (b.length == 0)
|
||||
return [[]];
|
||||
iRNG[][] res;
|
||||
foreach (c; Comb.On(s, b[0]))
|
||||
foreach (r; p(setDiff(s, c), b[1 .. $]))
|
||||
res ~= c.dup ~ r;
|
||||
return res;
|
||||
}
|
||||
|
||||
return p(sum, blockSize);
|
||||
}
|
||||
|
||||
void main(string[] args) {
|
||||
auto b = args.length > 1 ?
|
||||
args[1 .. $].map!(to!int)().array() :
|
||||
[2, 0, 2];
|
||||
|
||||
foreach (p; orderPart(b))
|
||||
writeln(p);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue