def find_all_subsets(s):
"""
find all subsets of a set, except for the empty set
:param s: a set represented by a list
:return: a list of subsets
"""
subsets = []
N = np.power(2,len(s))
for n in range(N-1): # each number represent a subset
set = [] # the subset corresponding to n
binary_ind = np.binary_repr(n+1) # binary
for idx in range(1,len(binary_ind)+1): # each bit of the binary number
if binary_ind[-idx] == '1': # '1' means to add the element corresponding to that bit
set.append(s[-idx])
subsets.append(set)
return subsets
评论列表
文章目录