find_set.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:dmcp 作者: cvxgrp 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号