Skip to content

Instantly share code, notes, and snippets.

@evidanary
Created June 16, 2017 22:01
Show Gist options
  • Save evidanary/020082a092344a18f97c8c344dd5d1e7 to your computer and use it in GitHub Desktop.
Save evidanary/020082a092344a18f97c8c344dd5d1e7 to your computer and use it in GitHub Desktop.
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