Anonymous user
Permutations: Difference between revisions
Updated first D entry
(Updated first D entry) |
|||
Line 898:
=={{header|D}}==
===Simple Eager version===
Compile with -version=permutations1_main to see the output.
<lang d>T[][] permutations(T)(T[] items) pure nothrow {▼
▲T[][] permutations(T)(T[] items) {
T[][] result;
void perms(T[] s, T[] prefix=[]) nothrow {
if (s.length)
foreach (immutable i, immutable c; s)
perms(s[0 .. i] ~ s[i+1 .. $], prefix ~ c);
else
Line 916 ⟶ 915:
}
version (permutations1_main) {
void main() {
writefln("%(%s\n%)", [1, 2, 3].permutations);
}
}</lang>
{{out}}
Line 927 ⟶ 928:
[3, 1, 2]
[3, 2, 1]</pre>
===Fast Lazy Version===
Compiled with <code>-version=permutations2_main</code> produces the same output:
|