Created
June 24, 2015 20:59
-
-
Save chrisnatali/027c47dd37e5f5ed046e to your computer and use it in GitHub Desktop.
iterative js powerset
This file contains hidden or 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 powerset(input) { | |
var result = []; | |
var selections; // bitmap of current subset | |
for(selections = 0; selections < Math.pow(2, input.length); selections++) { | |
var subset = []; | |
var index; // index into bitmap we're looking for | |
for(index = 0; index < input.length; index++) { | |
if(selections & (1 << index)) { | |
subset.push(input[index]); | |
} | |
} | |
result.push(subset); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment