How to resolve the algorithm Ordered partitions step by step in the D programming language
How to resolve the algorithm Ordered partitions step by step in the D programming language
Table of Contents
Problem Statement
In this task we want to find the ordered partitions into fixed-size blocks. This task is related to Combinations in that it has to do with discrete mathematics and moreover a helper function to compute combinations is (probably) needed to solve this task.
p a r t i t i o n s (
a r g
1
,
a r g
2
, . . . ,
a r g
n
)
{\displaystyle partitions({\mathit {arg}}{1},{\mathit {arg}}{2},...,{\mathit {arg}}_{n})}
should generate all distributions of the elements in
{ 1 , . . . ,
Σ
i
1
n
a r g
i
}
{\displaystyle {1,...,\Sigma {i=1}^{n}{\mathit {arg}}{i}}}
into
n
{\displaystyle n}
blocks of respective size
a r g
1
,
a r g
2
, . . . ,
a r g
n
{\displaystyle {\mathit {arg}}{1},{\mathit {arg}}{2},...,{\mathit {arg}}_{n}}
. Example 1:
p a r t i t i o n s ( 2 , 0 , 2 )
{\displaystyle partitions(2,0,2)}
would create: Example 2:
p a r t i t i o n s ( 1 , 1 , 1 )
{\displaystyle partitions(1,1,1)}
would create: Note that the number of elements in the list is (see the definition of the binomial coefficient if you are not familiar with this notation) and the number of elements remains the same regardless of how the argument is permuted (i.e. the multinomial coefficient). Also,
p a r t i t i o n s ( 1 , 1 , 1 )
{\displaystyle partitions(1,1,1)}
creates the permutations of
{ 1 , 2 , 3 }
{\displaystyle {1,2,3}}
and thus there would be
3 !
6
{\displaystyle 3!=6}
elements in the list. Note: Do not use functions that are not in the standard library of the programming language you use. Your file should be written so that it can be executed on the command line and by default outputs the result of
p a r t i t i o n s ( 2 , 0 , 2 )
{\displaystyle partitions(2,0,2)}
. If the programming language does not support polyvariadic functions pass a list as an argument. Notation Here are some explanatory remarks on the notation used in the task description:
{ 1 , … , n }
{\displaystyle {1,\ldots ,n}}
denotes the set of consecutive numbers from
1
{\displaystyle 1}
to
n
{\displaystyle n}
, e.g.
{ 1 , 2 , 3 }
{\displaystyle {1,2,3}}
if
n
3
{\displaystyle n=3}
.
Σ
{\displaystyle \Sigma }
is the mathematical notation for summation, e.g.
Σ
i
1
3
i
6
{\displaystyle \Sigma _{i=1}^{3}i=6}
(see also [1]).
a r g
1
,
a r g
2
, . . . ,
a r g
n
{\displaystyle {\mathit {arg}}{1},{\mathit {arg}}{2},...,{\mathit {arg}}_{n}}
are the arguments — natural numbers — that the sought function receives.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Ordered partitions step by step in the D programming language
Source code in the d programming language
import std.stdio, std.algorithm, std.range, std.array, std.conv,
combinations3;
alias iRNG = int[];
iRNG[][] orderPart(iRNG blockSize...) {
iRNG tot = iota(1, 1 + blockSize.sum).array;
iRNG[][] p(iRNG s, in iRNG b) {
if (b.empty)
return [[]];
iRNG[][] res;
foreach (c; s.combinations(b[0]))
foreach (r; p(setDifference(s, c).array, b.dropOne))
res ~= c.dup ~ r;
return res;
}
return p(tot, blockSize);
}
void main(in string[] args) {
auto b = args.length > 1 ? args.dropOne.to!(int[]) : [2, 0, 2];
writefln("%(%s\n%)", b.orderPart);
}
import core.stdc.stdio;
void genBits(size_t N)(ref uint[N] bits, in ref uint[N] parts,
uint mask, uint all, uint res, uint n, uint pid)
nothrow @nogc {
static void showPart(in uint x) nothrow @nogc {
'['.putchar;
for (uint i = 0; (1 << i) <= x; i++)
if (x & (1 << i))
printf("%d ", i + 1);
']'.putchar;
}
while (!n) {
bits[pid] = res;
pid++;
if (pid == N) {
foreach (immutable b; bits)
showPart(b);
'\n'.putchar;
return;
}
all &= ~res;
mask = all;
res = 0;
n = parts[pid];
}
while (mask) {
immutable uint i = mask & -int(mask);
mask &= ~i;
genBits(bits, parts, mask, all, res | i, n - 1, pid);
}
}
void main() nothrow @nogc {
immutable uint[3] parts = [2, 0, 2];
uint m = 1;
foreach (immutable p; parts)
m <<= p;
uint[parts.length] bits;
genBits(bits, parts, m - 1, m - 1, 0, parts[0], 0);
}
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the Tcl programming language
You may also check:How to resolve the algorithm Camel case and snake case step by step in the Wren programming language
You may also check:How to resolve the algorithm Product of min and max prime factors step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Scilab programming language
You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the C++ programming language