Power Set – algorithmic and fast

class Solution: 
    # @param A : list of integers 
    # @return a list of list of integers 
    def subsets(self, A):     
        if len(A) == 0:         
            return [[]]     
        h, t = A[0], A[1:]     
        subsets_excluding_h = self.subsets(t)     
        subsets_including_h = [sorted([h] + ss) for ss in subsets_excluding_h]     
    return sorted(subsets_including_h + subsets_excluding_h)

1. Recursive method to find all subsets of a given list
2. Python 3
3. It's quite fast.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s