https://github.com/samchon/cagen
Number of Case Generator
https://github.com/samchon/cagen
Last synced: about 1 year ago
JSON representation
Number of Case Generator
- Host: GitHub
- URL: https://github.com/samchon/cagen
- Owner: samchon
- License: mit
- Created: 2018-08-30T04:51:55.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-09-22T07:13:55.000Z (over 2 years ago)
- Last Synced: 2025-04-11T10:06:44.956Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 56.6 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cagen
[](https://github.com/samchon/cagen/blob/master/LICENSE)
[](https://www.npmjs.com/package/cagen)
[](https://www.npmjs.com/package/cagen)
[](https://github.com/samchon/cagen/actions?query=workflow%3Abuild)
Number of Case Generator for TypeScript.
Symbol | Class
--------------------------|----------------------
A x B x ... x Z | CartesianProduct
n! | Factorial
nPr | Permutation
n∏r | RepeatedPermutation
nHr | RepeatedCombination
nCr | Combination
- [API Documents](http://samchon.github.io/cagen/api)
- [Guide Documents](https://github.com/samchon/cagen/wiki)
## Usage
### Installation
```bash
npm install --save cagen
```
### Common Features
```typescript
namespace cagen.base
{
export interface IForwardGenerator
{
// FREQUENCE ACCESSORS
size(): number;
begin(): Iterator;
end(): Iterator;
// ES2015, THEN FOR-OF ITERATION IS ALSO POSSIBLE
[Symbol.iterator]: IterableIterator;
}
export interface Iterator
{
readonly value: number[];
next(): Iterator;
equals(obj: Iterator): boolean;
}
}
```
```typescript
import { CartesianProduct } from "cagen";
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();
```
```typescript
for (let aCase of generator)
console.log(" -", aCase);
```
### Random Accessor
```typescript
namespace cagen.base
{
export abstract class ArrayGenerator
implements IForwardGenerator
{
at(index: number): Array;
}
}
```
Most of Case Generator classes, except combination classes, 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~~
- ~~RepeatedCombination~~
```typescript
import { Permutation } from "cagen";
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 std.OutOfRange error.
}
main();
```