Permutations: Difference between revisions

84 bytes removed ,  11 months ago
m
Line 2,965:
* Implements permutations without repetition.
*/
module Permutations {
static Int[][] permut(Int items) {
{
static Int[][] permut(Int if (items <= 1) {
{
if (items <= 1)
{
// 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) {
for (Int[] suffix : pattern)
{
result.add(new Int[items](i -> i == 0 ? prefix : (prefix + suffix[i-1] + 1) % items));
}
}
return result;
}
return {result;
{}
 
void run() {
{
@Inject Console console;
console.print($"permut(3) = {permut(3)}");
}
}
}
</syntaxhighlight>
 
162

edits