-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerSet.py
50 lines (34 loc) · 1.08 KB
/
powerSet.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
def convertArrToString(arr):
st=""
for i in arr:
st = st + str(i)
return st
def powerSet(arr):
setall =set()
print(setall)
helper(setall,[],arr,0)
return setall
def helper(setall,prev,arr,index):
if len(prev) == 3 or index >2:
setall.add(convertArrToString(prev))
return
including = prev + [arr[index]]
excluding = prev
setall.add(convertArrToString(including))
setall.add(convertArrToString(excluding))
helper(setall,including,arr,index+1)
helper(setall,excluding,arr,index+1)
def powerSet_Short_Org(arr):
powerset=[[]]
for ele in arr:
for i in range(len(powerset)):
currentSubset= powerset[i]
powerset.append(currentSubset+[ele])
return powerset
def powerSet_Short(arr):
powerset=[[]]
for ele in arr:
for i in range(len(powerset)):
powerset.append(powerset[i]+[ele])
return powerset
print(powerSet_Short([1,2,3]))