Created
June 16, 2017 22:01
-
-
Save evidanary/020082a092344a18f97c8c344dd5d1e7 to your computer and use it in GitHub Desktop.
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
Powerset of a set is all possible sets from that set: i.e. imaging you have a 1/0 for each element indicating on off. Then size is 2^N | |
def powerset(arr) | |
partial = [] | |
pwr_helper(arr, partial, 0) | |
end | |
def pwr_helper(arr, partial, n) | |
if(n >= arr.size) | |
puts partial.inspect | |
return | |
end | |
partial.push(arr[n]) | |
pwr_helper(arr, partial, n+1) | |
partial.pop | |
pwr_helper(arr,partial,n+1) | |
end | |
powerset([1,2]) | |
powerset([1,2,3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment