Permutations: Difference between revisions
m
→{{header|Ecstasy}}
m (→{{header|K}}) |
|||
Line 2,965:
* Implements permutations without repetition.
*/
module Permutations {
static Int[][] permut(Int items) {
{▼
{▼
// with one item, there is a single permutation; otherwise there are no permutations
return items == 1 ? [[0]] : [];
// the "pattern" for all values but the first value in each permutation is
Line 2,982 ⟶ 2,979:
// the first digit
Int[][] result = new Int[][];
for (Int prefix : 0 ..< items) {
for (Int[] suffix : pattern) {
result.add(new Int[items](i -> i == 0 ? prefix : (prefix + suffix[i-1] + 1) % items));
}
}
void run() {
@Inject Console console;
console.print($"permut(3) = {permut(3)}");
}
}
</syntaxhighlight>
|