Skip to content

Instantly share code, notes, and snippets.

@abdulapopoola
Created January 27, 2015 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdulapopoola/fe8acc93066fc99b054a to your computer and use it in GitHub Desktop.
Save abdulapopoola/fe8acc93066fc99b054a to your computer and use it in GitHub Desktop.
PowerSet of a set in 16 lines of JavaScript
function powerSet(s) {
if(s.length === 0){
return [[]];
}
var headOfList = s.splice(0,1)[0];
var powerSetOfListTail = powerSet(s);
var powerSetOfListTailWithHead = powerSetOfListTail.map(function(p) {
var cpy = p.slice();
cpy.push(headOfList);
return cpy;
});
return powerSetOfListTail.concat(powerSetOfListTailWithHead);
}
powerSet([1,2,3]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment