-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Generating_Power_Set.py
71 lines (58 loc) · 1.88 KB
/
Generating_Power_Set.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Problem Statement :
Given a list of elements, return all possible subsets (the power set).
Description:
Power Set P(S) of a set S is the set of all subsets of S.
Power Set P(S) for a set S of <n> elements is 2^n elements.
Approach :
Iterate i from 0 to 2^n (as P(S) will have 2^n elements) and:
• Find the binary equivalent of i.
• Add the jth element from the array to the current subset when the jth
bit of binary(i) is 1.
• Add the subset to powerset list at end of each iteration.
"""
def binary(num, size):
"""
num: int
size: int
Takes in a number and returns it's binary equivalent.
Adds trailing zeroes at the beginning to make the
length of binary equivalent equal to size.
"""
binary_out = ''
while num > 0:
binary_out += str(num % 2)
num //= 2
binary_out += (size - len(binary_out)) * '0'
return binary_out[::-1]
def subsets(nums):
"""
nums: List[int] of unique elements
Returns a list of all possible subsets
"""
n = len(nums)
powerset = []
for i in range(2 ** n):
binary_eq = binary(i, n)
subset = []
for j in range(n):
if binary_eq[j] == '1':
subset.append(nums[j])
powerset.append(subset)
return powerset
if __name__ == "__main__":
list_of_values = input("Enter values seperated by space: ").split()
print("Power Set:", subsets(list_of_values))
# Sample Input-Output:
# Sample Input 1:
# Enter values seperated by space: 1 2 3
# Sample Output 1:
# Power Set: [[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]
# Sample Input 2:
# Enter values seperated by space: 1 2 -1 0
# Sample Output 2:
# Power Set: [[], [0], [-1], [-1, 0], [2], [2, 0], [2, -1], [2, -1, 0],
# [1], [1, 0], [1, -1], [1, -1, 0], [1, 2], [1, 2, 0],
# [1, 2, -1], [1, 2, -1, 0]]
# Time Complexity: O(N * 2*N)
# Space Complexity: O(2*N)