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; + } +}