Created
November 7, 2010 09:32
-
-
Save ajalab/666020 to your computer and use it in GitHub Desktop.
Permutation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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