From c7bc4d8f585672ad0b444703aee912798aeb087b Mon Sep 17 00:00:00 2001 From: mystixxx <77647824+mystixxx@users.noreply.github.com> Date: Mon, 26 Dec 2022 01:59:00 +0100 Subject: [PATCH] solving unrelated issue - bounty --- src/combine.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/combine.ts diff --git a/src/combine.ts b/src/combine.ts new file mode 100644 index 0000000..1cbba14 --- /dev/null +++ b/src/combine.ts @@ -0,0 +1,15 @@ +function combine(x: number, list: number[]): number[][] { + if (x === 0) { + return [[]]; + } else if (x === 1) { + return list.map(e => [e]); + } else { + const combinations: number[][] = []; + for (let i = 0; i < list.length; i++) { + for (const subcombination of combine(x - 1, list.slice(i + 1))) { + combinations.push([list[i], ...subcombination]); + } + } + return combinations; + } +}