combinations
combinations copied to clipboard
I typescrpted it...
const combo = <T>(a: T[], min?: number, max?: number): T[][] => {
min = min || 1;
max = Math.min(max || a.length, a.length);
const fn = (n: number, src: T[], got: T[], all: T[][]) => {
if (n == 0) {
if (got.length > 0) {
all[all.length] = got;
}
return;
}
for (let j = 0; j < src.length; j++) {
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
}
return;
};
const all = [];
for (let i = min; i <= max; i++) {
fn(i, a, [], all);
}
return all;
};
I also applied the max bug fix.