@math-x-ts/generix
Version:
This library tasks is to generate combinations and permutations
118 lines • 5.87 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Class to generate permutations with & without repetition.
*/
var Permutation = /** @class */ (function () {
function Permutation() {
}
/**
* Generates all possible permutations without repetition of a given array of elements,
* with the option to specify the minimum (n) and maximum (m) lengths of the generated permutations.
* The maximum must be less or equal to the number of elements.
*
* @template S - The type of the elements in the input array.
* @param {Array<S>} elements - The input array containing the elements to generate permutations for.
* @param {number} [n=elements.length] - The minimum number of elements in the generated permutations (inclusive).
* @param {number} [m=n] - The maximum number of elements in the generated permutations (inclusive).
* @param {Array<S>} [combo=[]] - An optional array to store the current combination during recursion.
* Should not be provided when calling the function initially.
*
* @returns {Array<Array<S>>} - An array of arrays, where each inner array is a permutation of the input elements.
*/
Permutation.generateWithoutRepetition = function (elements, n, m, combo) {
if (n === void 0) { n = elements.length; }
if (m === void 0) { m = n; }
if (combo === void 0) { combo = []; }
var result = [];
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var rest = __spreadArray(__spreadArray([], elements.slice(0, i), true), elements.slice(i + 1, elements.length), true);
combo.push(element);
if (combo.length >= n && combo.length <= m) {
result.push(__spreadArray([], combo, true));
}
if (combo.length <= m) {
result.push.apply(result, Permutation.generateWithoutRepetition(rest, n, m, combo));
}
combo.pop();
}
return result;
};
/**
* Generates all possible permutations 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 permutations for.
* @param {number} [n=elements.length] - The minimum length of the generated permutations (inclusive).
* @param {number} [m=n] - The maximum length of the generated permutations (inclusive).
* @returns {Array<Array<S>>} - An array of arrays, where each inner array is a permutation of the input elements.
*/
Permutation.withoutRepetition = function (elements, n, m) {
if (n === void 0) { n = elements.length; }
if (m === void 0) { m = n; }
var result = Permutation.generateWithoutRepetition(elements, n, m, []);
return result.sort(function (a, b) { return a.length - b.length; });
};
/**
* Generates all possible permutations with 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 elements in the input array and resulting permutations.
* @param {Array<S>} elements A set of elements used during the generation process.
* @param {number} [n=elements.length] The minimum number of elements in the generated permutations (inclusive).
* @param {number} [m=n] The maximum number of elements in the generated permutations (inclusive).
*
* @returns {Array<Array<S>>} An array of arrays containing all the possible permutations.
*
* @example
* // Generate permutations with repetition for the elements 'a', 'b', and 'c', with a size between 2 and 3
* const result = Permutations.withRepetition(['a', 'b', 'c'], 2, 3);
* [['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'b'], ['b', 'c'], ['c', 'c'], ['a', 'a', 'a'], ['a', 'a', 'b'], ...]
*/
Permutation.withRepetition = function (elements, n, m) {
if (n === void 0) { n = elements.length; }
if (m === void 0) { m = n; }
var combinations = [];
var result = [];
for (var _i = 0, elements_1 = elements; _i < elements_1.length; _i++) {
var s = elements_1[_i];
combinations.push([s]);
if (n <= 1) {
result.push([s]);
}
}
var startIndex = 0;
var endIndex = combinations.length;
for (var i = 0; i < m - 1; i++) {
for (var j = startIndex; j < endIndex; j++) {
var c = combinations[j];
var newCombos = [];
for (var k = 0; k < elements.length; k++) {
newCombos.push(__spreadArray(__spreadArray([], c, true), [elements[k]], false));
if (c.length + 1 >= n && c.length - 1 <= m) {
result.push(__spreadArray(__spreadArray([], c, true), [elements[k]], false));
}
}
combinations.push.apply(combinations, newCombos);
}
startIndex = endIndex;
endIndex = combinations.length;
}
return result;
};
;
return Permutation;
}());
exports.default = Permutation;
//# sourceMappingURL=Permutation.js.map