Skip to content

Instantly share code, notes, and snippets.

@tlylt
Created May 17, 2020 16:01
Show Gist options
  • Save tlylt/4338750037c08e33a1ecbf1c48174b35 to your computer and use it in GitHub Desktop.
Save tlylt/4338750037c08e33a1ecbf1c48174b35 to your computer and use it in GitHub Desktop.
# Number of Combinations = 2**(length of input set)
# Makes use of binary
def PowerSet(input):
ans = []
numOfComb = 2 ** len(input)
for combidx in range(numOfComb): # 0 -> numOfComb
temp = []
for setidx in range(len(input)): # 0 -> len(input)
if combidx & 1 << setidx: # bitwise operation e.g. 0 & 1 << 0 =>0
temp.append(input[setidx])
ans.append(temp)
return ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment