Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Permutations/Ecstasy/permutations.ecstasy
Normal file
30
Task/Permutations/Ecstasy/permutations.ecstasy
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* Implements permutations without repetition.
|
||||
*/
|
||||
module Permutations {
|
||||
static Int[][] permut(Int items) {
|
||||
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
|
||||
// derived from the permutations of the next smaller number of items
|
||||
Int[][] pattern = permut(items - 1);
|
||||
|
||||
// build the list of all permutations for the specified number of items by iterating only
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void run() {
|
||||
@Inject Console console;
|
||||
console.print($"permut(3) = {permut(3)}");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue