Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Power set python

"""
This implementation demonstrates how 
to generate the power set of an array 
of unique integers.

Example: 
For following array: [1, 2]
The power set is the set of all subsets of 
the array. 
So, output would be: [[], [1], [2], [1, 2]]

Let n be the size of the array.

Time complexity: O(n*2^n)
Space complexity: O(n*2^n)
"""


def powerset(array):
    # Empty set is part of power set
    subsets = [[]]
    for element in array:
        # Add every element to existing subsets
        for idx in range(len(subsets)):
            subset = subsets[idx]
            subsets.append(subset + [element])
    return subsets


print(powerset([1, 2]))  # [[], [1], [2], [1, 2]]
Comment

PREVIOUS NEXT
Code Example
Python :: plt.plot width line 
Python :: install python glob module in windows 
Python :: time decorator python 
Python :: ctypes run as administrator 
Python :: cv2 draw box 
Python :: python loop through files in directory recursively 
Python :: show rows with a null value pandas 
Python :: python format 2 digits 
Python :: convert mp3 to wav python 
Python :: plot specific columns pandas 
Python :: desktop background change with python 
Python :: comment dériver une classe python 
Python :: python requests get title 
Python :: daphne heroku 
Python :: confidence intervals in python 
Python :: how to install gym 
Python :: dataframe slice by list of values 
Python :: dns request scapy 
Python :: torch summary 
Python :: record video with python 
Python :: how to calculate running time in python 
Python :: how to get the contents of a txt file in python 
Python :: python datetime module print 12 hour clock 
Python :: get video width and height cv2 
Python :: qtimer python 
Python :: dictionary sort python 
Python :: python copy a 2D list 
Python :: learn python the hard way pdf 
Python :: python check ram usage 
Python :: tqdm in for loop 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =