Problem: If you given a problem to generate subsets of given array , we can use PowerSet concept.
lets say Array = ['Red','Orange','Blue']
the power set is defined as 2 to power of len(Array). 2^3 here.
Run the binary counter from 0 to pow(2,len(Array))
def gen_subsets(list_attribs):
len_set = len(list_attribs)
power_set = 2 ** len_set
for i in range(power_set): # this generates the binary sequence
subsets = ""
for j in range(len_set): #iterate through the list_attribs positions and check bit is set
if i & (1 << j):
subsets += list_attribs[j] + ","
print(subsets[0:-1])
lets say Array = ['Red','Orange','Blue']
the power set is defined as 2 to power of len(Array). 2^3 here.
Run the binary counter from 0 to pow(2,len(Array))
Value of Counter Subset 000 -> Empty set 001 -> Red 011 -> Red,Orange 100 -> Blue 101 -> Red,Blue 110 -> Orange,Blue 111 -> Red,Orange,Blue
def gen_subsets(list_attribs):
len_set = len(list_attribs)
power_set = 2 ** len_set
for i in range(power_set): # this generates the binary sequence
subsets = ""
for j in range(len_set): #iterate through the list_attribs positions and check bit is set
if i & (1 << j):
subsets += list_attribs[j] + ","
print(subsets[0:-1])