2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-09-18 18:29:29 +00:00
|
|
|
// This module is browser compatible.
|
2021-07-15 21:37:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds all possible orders of all elements in the given array
|
2021-09-07 07:40:05 +00:00
|
|
|
* Ignores equality of elements, meaning this will always return the same
|
2021-07-15 21:37:53 +00:00
|
|
|
* number of permutations for a given length of input.
|
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the elements in the array.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @param inputArray The array to build permutations from.
|
|
|
|
*
|
|
|
|
* @returns An array of all possible permutations of the given array.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
2021-07-30 14:49:46 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { permutations } from "@std/collections/permutations";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2021-07-30 14:49:46 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* const numbers = [ 1, 2 ];
|
|
|
|
* const windows = permutations(numbers);
|
2021-07-15 21:37:53 +00:00
|
|
|
*
|
2021-08-10 08:46:14 +00:00
|
|
|
* assertEquals(windows, [
|
2022-11-25 11:40:23 +00:00
|
|
|
* [ 1, 2 ],
|
|
|
|
* [ 2, 1 ],
|
|
|
|
* ]);
|
2021-07-15 21:37:53 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2023-05-30 23:48:21 +00:00
|
|
|
export function permutations<T>(inputArray: Iterable<T>): T[][] {
|
2024-05-07 07:11:55 +00:00
|
|
|
const result: T[][] = [];
|
2023-05-30 23:48:21 +00:00
|
|
|
|
|
|
|
const array = [...inputArray];
|
|
|
|
|
|
|
|
const k = array.length;
|
2021-07-15 21:37:53 +00:00
|
|
|
|
2021-08-17 04:19:42 +00:00
|
|
|
if (k === 0) {
|
2024-05-07 07:11:55 +00:00
|
|
|
return result;
|
2021-07-15 21:37:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 04:19:42 +00:00
|
|
|
// Heap's Algorithm
|
|
|
|
const c = new Array<number>(k).fill(0);
|
2021-07-15 21:37:53 +00:00
|
|
|
|
2024-05-07 07:11:55 +00:00
|
|
|
result.push([...array]);
|
2021-07-15 21:37:53 +00:00
|
|
|
|
2021-08-17 04:19:42 +00:00
|
|
|
let i = 1;
|
2021-07-15 21:37:53 +00:00
|
|
|
|
2021-08-17 04:19:42 +00:00
|
|
|
while (i < k) {
|
2024-02-02 06:01:54 +00:00
|
|
|
if (c[i]! < i) {
|
2021-08-17 04:19:42 +00:00
|
|
|
if (i % 2 === 0) {
|
2024-02-02 06:01:54 +00:00
|
|
|
[array[0], array[i]] = [array[i], array[0]] as [T, T];
|
2021-07-15 21:37:53 +00:00
|
|
|
} else {
|
2024-02-02 06:01:54 +00:00
|
|
|
[array[c[i]!], array[i]] = [array[i], array[c[i]!]] as [T, T];
|
2021-07-15 21:37:53 +00:00
|
|
|
}
|
2021-08-17 04:19:42 +00:00
|
|
|
|
2024-05-07 07:11:55 +00:00
|
|
|
result.push([...array]);
|
2021-08-17 04:19:42 +00:00
|
|
|
|
2024-07-03 22:12:41 +00:00
|
|
|
c[i]! += 1;
|
2021-08-17 04:19:42 +00:00
|
|
|
i = 1;
|
|
|
|
} else {
|
|
|
|
c[i] = 0;
|
|
|
|
i += 1;
|
2021-07-15 21:37:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-07 07:11:55 +00:00
|
|
|
return result;
|
2021-07-15 21:37:53 +00:00
|
|
|
}
|