-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpower_set.py
39 lines (28 loc) · 962 Bytes
/
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
# Recursive Solution
# Time O(n * 2^n)
# Space O(n)
def generate_power_set(input_set):
def directed_power_set(to_be_selected, selected_so_far):
if to_be_selected == len(input_set):
power_set.append(list(selected_so_far))
return
directed_power_set(to_be_selected + 1, selected_so_far)
directed_power_set(to_be_selected + 1, selected_so_far + [input_set[to_be_selected]])
power_set = []
directed_power_set(0, [])
return power_set
print("Recursive Solution")
print(generate_power_set([0, 1, 2]))
# Iterative Solution
def generate_power_set_iter(A):
subsets = []
subsets.append([])
for i in range(len(A)):
curr_val = A[i]
for j in range(len(subsets)):
copy = subsets[j].copy()
copy.append(curr_val)
subsets.append(copy)
return subsets
print("\nIterative Solution")
print(generate_power_set_iter([0, 1, 2]))