Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zoubin/array-permutation
permutation util
https://github.com/zoubin/array-permutation
Last synced: 8 days ago
JSON representation
permutation util
- Host: GitHub
- URL: https://github.com/zoubin/array-permutation
- Owner: zoubin
- Created: 2015-05-19T11:52:32.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-07T02:08:50.000Z (over 7 years ago)
- Last Synced: 2024-10-13T14:53:50.527Z (about 1 month ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
Awesome Lists containing this project
README
# array-permutation
permutation utils. Return `Iterable` for iterating all permutations of an array.## Usage
```javascript
var permutation = require('array-permutation');
var range = permutation.range;
var random = permutation.random;
var shuffle = permutation.shuffle;
```
### range(n)Return [0,..,n-1]
### range(low, high, step)
Return [low, low + step, low + 2 * step,..., low + k * step], with `low + k * step < high`
### random(n)
Return an array populated with a random permutation of `range(n)`
### random(low, high, step)
Return an array populated with a random permutation of `range(low, high, step)`
### permutation(n)
Return an iterable, for iterating all permutations of `range(n)`
### permutation(low, high, step)
Return an iterable, for iterating all permutations of `range(low, high, step)`
### permutation([])
Return an iterable, for iterating all permutations of the given array.
### shuffle([])
Shuffle the given array, and return it.
## Example
### permutation
```javascript
var perm = require('..');
var iter = perm([1, 2, 3, 4]);
for (var p of iter) {
console.log(p);
}```
output:
```
⌘ node example/permutation.js
[ 1, 2, 3, 4 ]
[ 2, 1, 3, 4 ]
[ 2, 3, 1, 4 ]
[ 2, 3, 4, 1 ]
[ 1, 3, 2, 4 ]
[ 3, 1, 2, 4 ]
[ 3, 2, 1, 4 ]
[ 3, 2, 4, 1 ]
[ 1, 3, 4, 2 ]
[ 3, 1, 4, 2 ]
[ 3, 4, 1, 2 ]
[ 3, 4, 2, 1 ]
[ 1, 2, 4, 3 ]
[ 2, 1, 4, 3 ]
[ 2, 4, 1, 3 ]
[ 2, 4, 3, 1 ]
[ 1, 4, 2, 3 ]
[ 4, 1, 2, 3 ]
[ 4, 2, 1, 3 ]
[ 4, 2, 3, 1 ]
[ 1, 4, 3, 2 ]
[ 4, 1, 3, 2 ]
[ 4, 3, 1, 2 ]
[ 4, 3, 2, 1 ]
```### random
```javascript
var random = require('..').random;console.log(random(5)); // [ 3, 2, 4, 1, 0 ]
console.log(random(2, 5)); // [ 4, 3, 2 ]
console.log(random(1, 5, 2)); // [ 3, 1 ]
console.log(random([1, 2, 3, 4, 5])); // [ 4, 5, 3, 1, 2 ]
```output:
```
⌘ node example/random.js
[ 4, 0, 1, 2, 3 ]
[ 4, 2, 3 ]
[ 3, 1 ]
[ 3, 4, 1, 5, 2 ]
```