2.0.0 ⢠Published 6 years ago
number-of-cases v2.0.0
Number-of-Cases
Number of Case Generator for TypeScript (JavaScript).
| Symbol | Class |
|---|---|
| nPr | Permutation |
| n! | Factorial |
| nār | RepeatedPermutation |
| A x B x ... x Z | CartesianProduct |
| nCr | Combination |
Usage
Installation
npm install --save number-of-casesCommon Features
interface CaseGenerator<Iterator, ReverseIterator>
{
// FREQUENCE ACCESSORS
size(): number;
begin(): Iterator;
end(): Iterator;
// REVERSE ITERATION IS ALSO POSSIBLE
rbegin(): ReverseIterator;
rend(): ReverseIterator;
// ES2015, THEN FOR-OF ITERATION IS ALSO POSSIBLE
[Symbol.iterator]: IterableIterator<Array<number>>;
}
interface Iterator
{
get value(): Array<number>;
prev(): Iterator;
next(): Iterator;
equals(obj: Iterator): boolean;
}import { CartesianProduct } from "number-of-cases";
function main(): void
{
let generator = new CartesianProduct(5, 4); // 5C4
console.log("n(5C4) =", generator.size());
for (let it = generator.begin(); !it.equals(generator.end()); it = it.next())
{
let aCase: number[] = it.value;
console.log(" -", aCase);
}
}
main();for (let it = generator.rbegin(); !it.equals(generator.rend()); it = it.next())
{
let aCase: number[] = it.value;
console.log(" -", aCase);
}for (let aCase of generator)
console.log(" -", aCase);Random Accessor
interface RandomCaseGenerator<Iterator, ReverseIterator>
extends CaseGenerator<Iterator, ReverseIterator>
{
at(index: number): Array<number>;
}Most of Case Generator classes, except the Combination class, provide a random accessor at(). By that method at(), you can access to a specific case through not full iteration, but the special index number.
- Permutation
- Factorial
- RepeatedPermutation
- CartesianProduct
Combination
import { Permutation } from "number-of-cases";
function main(): void
{
let generator = new Permutation(7, 3);
console.log(generator.at(13));
console.log(generator.at(31));
console.log(generator.at(9999)); // throw an out_of_range error.
}
main();2.0.0
6 years ago
1.1.0
6 years ago
1.1.0-dev.20200107
6 years ago
1.1.0-dev.20191229
6 years ago
1.1.0-dev.20191224
6 years ago
1.1.0-dev.20191223
6 years ago
1.1.0-dev.20191216
6 years ago
1.1.0-dev.20191215
6 years ago
1.1.0-dev.20191213
6 years ago
1.0.1
7 years ago
1.0.0
7 years ago
0.0.0
7 years ago