combinations icon indicating copy to clipboard operation
combinations copied to clipboard

I typescrpted it...

Open jonathan-meyer opened this issue 3 years ago • 1 comments

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

jonathan-meyer avatar Oct 08 '21 15:10 jonathan-meyer

I also applied the max bug fix.

jonathan-meyer avatar Oct 08 '21 15:10 jonathan-meyer