Skip to content

Commit

Permalink
feat(combinations): Add a combination class for combination generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dopey2 committed May 7, 2023
1 parent 438a2fb commit 8d05e55
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions packages/combination/src/Combination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

/**
* Used to generate combinations
*/
export default class Combination {

/**
* The concrete implementation of the combination algorithm.
*
* @template S - The type of the elements in the input array.
* @param {Array<S>} elements - The input array containing the elements to generate combination for.
* @param {number} startIndex - The starting index for.
* @param {Array<Array<S>>} currentCombination - The accumulated combination during the recursion.
* @param {number} n - The minimum length of the generated combination (inclusive).
* @param {number} m - The maximum length of the generated combination (inclusive).
*
* @returns {Array<Array<S>>} - An array of arrays, where each inner array is a combination of the input elements.
*/
private static generateCombination<S>(
elements: Array<S>,
startIndex: number,
currentCombination: S[],
n: number,
m: number
): Array<Array<S>> {
const result: Array<Array<S>> = [];

if (currentCombination.length >= n && currentCombination.length <= m) {
result.push(currentCombination.slice());
}

if(currentCombination.length >= m) {
return result;
}

for (let i = startIndex; i < elements.length; i++) {
currentCombination.push(elements[i]);
const combinations = Combination.generateCombination(elements, i + 1, currentCombination, n, m);
combinations && result.push(...combinations);
currentCombination.pop();
}

return result;
}

/**
* Generates all possible combinations without repetition of a given array of elements, with the option to specify
* the minimum (n) and maximum (m) lengths of the generated permutations.
*
* @template S - The type of the elements in the input array.
* @param {Array<S>} elements - The input array containing the elements to generate combination for.
* @param {number} [n=elements.length] - The minimum length of the generated combination (inclusive).
* @param {number} [m=n] - The maximum length of the generated combination (inclusive).
* @returns {Array<Array<S>>} - An array of arrays, where each inner array is a combination of the input elements.
*/
static withoutRepetition<S>(elements: S[], n: number, m: number = n): S[][] {
const combinations = Combination.generateCombination(elements,0, [], n, m);
return combinations.sort((a, b) => a.length - b.length);
}
}

0 comments on commit 8d05e55

Please sign in to comment.