Package Exports
- generatorics
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (generatorics) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Generatorics
A combinatorics library for JavaScript utilizing ES2015 generators. Generate combinations, permutations, and power sets of arrays or strings.
- Node
npm install generatoricsvar G = require('generatorics');- Browser
bower install generatorics<script src="file/path/to/generatorics.js"></script>Usage
Power Set
for (var subset of G.powerSet(['a', 'b', 'c'])) {
console.log(subset);
}
// [ ]
// [ 'a' ]
// [ 'a', 'b' ]
// [ 'a', 'b', 'c' ]
// [ 'a', 'c' ]
// [ 'b' ]
// [ 'b', 'c' ]
// [ 'c' ]permutation
for (var perm of G.permutation(['a', 'b', 'c'], 2)) {
console.log(perm);
}
// [ 'a', 'b' ]
// [ 'a', 'c' ]
// [ 'b', 'a' ]
// [ 'b', 'c' ]
// [ 'c', 'a' ]
// [ 'c', 'b' ]
for (var perm of G.permutation(['a', 'b', 'c'])) { // assumes full length of array
console.log(perm);
}
// [ 'a', 'b', 'c' ]
// [ 'a', 'c', 'b' ]
// [ 'b', 'a', 'c' ]
// [ 'b', 'c', 'a' ]
// [ 'c', 'b', 'a' ]
// [ 'c', 'a', 'b' ]combination
for (var comb of G.combination(['a', 'b', 'c'], 2)) {
console.log(comb);
}
// [ 'a', 'b' ]
// [ 'a', 'c' ]
// [ 'b', 'c' ]For efficiency, each array being yielded is the same one being mutated on each iteration.
var combs = [];
for (var comb of G.combination(['a', 'b', 'c'], 2)) {
combs.push(comb);
}
console.log(combs);
// [ [ 'b', 'c' ], [ 'b', 'c' ], [ 'b', 'c' ] ]You can clone if necessary.
var combs = [];
for (var comb of G.combination(['a', 'b', 'c'], 2)) {
combs.push(comb.slice()); // slice with no args is an idiomatic way to clone an array
}
console.log(combs);
// [ [ 'a', 'b' ], [ 'a', 'c' ], [ 'b', 'c' ] ]permutation of combination
for (var perm of G.permutationCombination(['a', 'b', 'c'])) {
console.log(perm);
}
// [ ]
// [ 'a' ]
// [ 'a', 'b' ]
// [ 'a', 'b', 'c' ]
// [ 'a', 'c' ]
// [ 'a', 'c', 'b' ]
// [ 'b' ]
// [ 'b', 'a' ]
// [ 'b', 'a', 'c' ]
// [ 'b', 'c' ]
// [ 'b', 'c', 'a' ]
// [ 'c' ]
// [ 'c', 'a' ]
// [ 'c', 'a', 'b' ]
// [ 'c', 'b' ]
// [ 'c', 'b', 'a' ]cartesian product
for (var prod of G.cartesian([0, 1, 2], [0, 10, 20], [0, 100, 200])) {
console.log(prod);
}
// [ 0, 0, 0 ], [ 0, 0, 100 ], [ 0, 0, 200 ]
// [ 0, 10, 0 ], [ 0, 10, 100 ], [ 0, 10, 200 ]
// [ 0, 20, 0 ], [ 0, 20, 100 ], [ 0, 20, 200 ]
// [ 1, 0, 0 ], [ 1, 0, 100 ], [ 1, 0, 200 ]
// [ 1, 10, 0 ], [ 1, 10, 100 ], [ 1, 10, 200 ]
// [ 1, 20, 0 ], [ 1, 20, 100 ], [ 1, 20, 200 ]
// [ 2, 0, 0 ], [ 2, 0, 100 ], [ 2, 0, 200 ]
// [ 2, 10, 0 ], [ 2, 10, 100 ], [ 2, 10, 200 ]
// [ 2, 20, 0 ], [ 2, 20, 100 ], [ 2, 20, 200 ]base N
for (var num of G.baseN(['a', 'b', 'c'])) {
console.log(num);
}
// [ 'a', 'a', 'a' ], [ 'a', 'a', 'b' ], [ 'a', 'a', 'c' ]
// [ 'a', 'b', 'a' ], [ 'a', 'b', 'b' ], [ 'a', 'b', 'c' ]
// [ 'a', 'c', 'a' ], [ 'a', 'c', 'b' ], [ 'a', 'c', 'c' ]
// [ 'b', 'a', 'a' ], [ 'b', 'a', 'b' ], [ 'b', 'a', 'c' ]
// [ 'b', 'b', 'a' ], [ 'b', 'b', 'b' ], [ 'b', 'b', 'c' ]
// [ 'b', 'c', 'a' ], [ 'b', 'c', 'b' ], [ 'b', 'c', 'c' ]
// [ 'c', 'a', 'a' ], [ 'c', 'a', 'b' ], [ 'c', 'a', 'c' ]
// [ 'c', 'b', 'a' ], [ 'c', 'b', 'b' ], [ 'c', 'b', 'c' ]
// [ 'c', 'c', 'a' ], [ 'c', 'c', 'b' ], [ 'c', 'c', 'c' ]Documentation
G
- G
- .factorial(n) ⇒
Number - .factoradic(n) ⇒
Array - .P(n, r) ⇒
Number - .C(n, r) ⇒
Number - .combination(arr, [size]) ⇒
Generator - .permutation(arr, [size]) ⇒
Generator - .powerSet(arr) ⇒
Generator - .permutationCombination(arr) ⇒
Generator - .baseN(arr, [size]) ⇒
Generator - .cartesian(...sets) ⇒
Generator
- .factorial(n) ⇒
G.factorial(n) ⇒ Number
Calculates a factorial
Kind: static method of G
Returns: Number - n!
| Param | Type | Description |
|---|---|---|
| n | Number |
The number to operate the factorial on. |
G.factoradic(n) ⇒ Array
Converts a number to the factorial number system. Digits are in least significant order.
Kind: static method of G
Returns: Array - digits of n in factoradic in least significant order
| Param | Type | Description |
|---|---|---|
| n | Number |
Integer in base 10 |
G.P(n, r) ⇒ Number
Calculates the number of possible permutations of "r" elements in a set of size "n".
Kind: static method of G
Returns: Number - n P r
| Param | Type | Description |
|---|---|---|
| n | Number |
Number of elements in the set. |
| r | Number |
Number of elements to choose from the set. |
G.C(n, r) ⇒ Number
Calculates the number of possible combinations of "r" elements in a set of size "n".
Kind: static method of G
Returns: Number - n C r
| Param | Type | Description |
|---|---|---|
| n | Number |
Number of elements in the set. |
| r | Number |
Number of elements to choose from the set. |
G.combination(arr, [size]) ⇒ Generator
Generates all combinations of a set.
Kind: static method of G
Returns: Generator - yields each combination as an array
| Param | Type | Default | Description |
|---|---|---|---|
| arr | Array | String |
The set of elements. | |
| [size] | Number |
arr.length |
Number of elements to choose from the set. |
G.permutation(arr, [size]) ⇒ Generator
Generates all permutations of a set.
Kind: static method of G
Returns: Generator - yields each permutation as an array
| Param | Type | Default | Description |
|---|---|---|---|
| arr | Array | String |
The set of elements. | |
| [size] | Number |
arr.length |
Number of elements to choose from the set. |
G.powerSet(arr) ⇒ Generator
Generates all possible subsets of a set (a.k.a. power set).
Kind: static method of G
Returns: Generator - yields each subset as an array
| Param | Type | Description |
|---|---|---|
| arr | Array | String |
The set of elements. |
G.permutationCombination(arr) ⇒ Generator
Generates the permutation of the combinations of a set.
Kind: static method of G
Returns: Generator - yields each permutation as an array
| Param | Type | Description |
|---|---|---|
| arr | Array | String |
The set of elements. |
G.baseN(arr, [size]) ⇒ Generator
Generates all possible "numbers" from the digits of a set.
Kind: static method of G
Returns: Generator - yields all digits as an array
| Param | Type | Default | Description |
|---|---|---|---|
| arr | Array | String |
The set of digits. | |
| [size] | Number |
arr.length |
How many digits will be in the numbers. |
G.cartesian(...sets) ⇒ Generator
Generates the cartesian product of the sets.
Kind: static method of G
Returns: Generator - yields each product as an array
| Param | Type | Description |
|---|---|---|
| ...sets | Array | String |
Variable number of sets of n elements. |