-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailycoding037.py
59 lines (45 loc) · 1.32 KB
/
dailycoding037.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
"""
This problem was asked by Google.
The power set of a set is the set of all its subsets.
Write a function that, given a set, generates its power set.
solution:
Use a divide and conquer approach by recusively including all subsets
missing one element from the original set, e.g.
{1,2,3} ->
{2,3} ->
{2}, {3}
{1,3} ->
{1}, {3}
{1,2} ->
{1}, {2}
To avoid adding duplicates to power set, use the powerset as a memoization
table, passing it along every recursion and only adding to it if a subset
is not already in it.
Runs in O(2^N) time since that is the cardinality of the power set
"""
def subset(nums, k, size):
return [nums[i] for i in range(size) if i != k]
def powerset_rec(nums, pset):
if set(nums) in pset:
return
pset.append(set(nums))
for i in range(len(nums)):
s = subset(nums, i, len(nums))
powerset_rec(s, pset)
def powerset(nums):
pset = []
powerset_rec(list(nums), pset)
return pset
def main():
tests = (
(set(), [set()]),
({1,2,3}, [set(),{1},{2},{3},{1,2},{2,3},{1,3},{1,2,3}]),
({1}, [set(), {1}]),
({1,2}, [set(), {1,2}, {1}, {2}])
)
if all([s in t[1] for s in powerset(t[0])] for t in tests):
print("Passed")
else:
print("Failed")
if __name__ == '__main__':
main()