Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
heaps-permute
Advanced tools
Readme
A JavaScript implementation of Heap's efficient Permutation Algorithm.
This algorithm is "efficient", but still runs in factorial time. It will probably run out of memory if you try it on an array longer than 10 items. For example:
n = 7
will finish within 5 milliseconds (5040 permutations)n = 8
will finish within 50 milliseconds (40,320 permutations)n = 9
will finish within 500 milliseconds (362,880 permutations)n = 10
will finish within 6000 milliseconds (328,800 permutations)$ npm install heaps-permute
var permute = require('heaps-permute');
This returns all the permutations for the given array.
permute([1, 2, 3]);
// returns
[
[ 1, 2, 3 ],
[ 2, 1, 3 ],
[ 3, 1, 2 ],
[ 1, 3, 2 ],
[ 2, 3, 1 ],
[ 3, 2, 1 ]
];
permute(['a', 'b', 'c', 'd'])
// returns
[
[ 'a', 'b', 'c', 'd' ],
[ 'b', 'a', 'c', 'd' ],
[ 'c', 'a', 'b', 'd' ],
[ 'a', 'c', 'b', 'd' ],
[ 'b', 'c', 'a', 'd' ],
[ 'c', 'b', 'a', 'd' ],
[ 'd', 'b', 'c', 'a' ],
[ 'b', 'd', 'c', 'a' ],
[ 'c', 'd', 'b', 'a' ],
[ 'd', 'c', 'b', 'a' ],
[ 'b', 'c', 'd', 'a' ],
[ 'c', 'b', 'd', 'a' ],
[ 'd', 'a', 'c', 'b' ],
[ 'a', 'd', 'c', 'b' ],
[ 'c', 'd', 'a', 'b' ],
[ 'd', 'c', 'a', 'b' ],
[ 'a', 'c', 'd', 'b' ],
[ 'c', 'a', 'd', 'b' ],
[ 'd', 'a', 'b', 'c' ],
[ 'a', 'd', 'b', 'c' ],
[ 'b', 'd', 'a', 'c' ],
[ 'd', 'b', 'a', 'c' ],
[ 'a', 'b', 'd', 'c' ],
[ 'b', 'a', 'd', 'c' ]
];
This method invokes cb
each time a unique permutation is found. The new permutation will be the only argument to the callback.
permute.callback([1, 2, 3], console.log);
// will print to the console:
[ 1, 2, 3 ]
[ 2, 1, 3 ]
[ 3, 1, 2 ]
[ 1, 3, 2 ]
[ 2, 3, 1 ]
[ 3, 2, 1 ]
Do not pass this method a third argument: it's needed for an internal counter.
To run the test suite, first install the dependencies, then run npm test
:
$ npm install
$ npm test
To test code coverage:
$ npm run cover
FAQs
A JavaScript implementation of Heap's efficient Permutation Algorithm
The npm package heaps-permute receives a total of 41 weekly downloads. As such, heaps-permute popularity was classified as not popular.
We found that heaps-permute demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.