Skip to content

Instantly share code, notes, and snippets.

@ajalab
Created November 7, 2010 09:32
Show Gist options
  • Save ajalab/666020 to your computer and use it in GitHub Desktop.
Save ajalab/666020 to your computer and use it in GitHub Desktop.
Permutation
function choose(arr, dup, len) {
var retarr = [], i, n, group = {}, gpos, p;
arr = clone(arr);
dup = !!dup;
len = len || arr.length;
function sub(arr, buf) {
var i, c, subarr, subbuf;
for (i = 0; i < arr.length; i++) {
subarr = clone(arr);
subbuf = clone(buf);
c = subarr[i];
gpos = group[c];
dup ? subbuf.push(c) : subbuf.push(subarr.splice(i, 1)[0]);
if (subbuf.length === len) {
retarr.push(subbuf);
} else {
sub(subarr, subbuf);
}
}
}
sub(arr, []);
return retarr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment