File tree 1 file changed +28
-0
lines changed
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -66,3 +66,31 @@ def powerset(array):
66
66
def Powerset (array ):
67
67
# Call the helper function to generate the powerset starting from the last index
68
68
return powerset (array , len (array ) - 1 )
69
+
70
+
71
+ # Iterative approach
72
+ def powersetIterative (array ):
73
+ # Initialize the powerset with the empty subset
74
+ subset = [[]]
75
+
76
+ # Iterate over each element in the input array
77
+ for ele in array :
78
+ # Get the current length of the subset
79
+ length = len (subset )
80
+
81
+ # Iterate over each existing subset
82
+ for i in range (length ):
83
+ # Get the current subset
84
+ currentSubset = subset [i ]
85
+
86
+ # Create a new subset by making a copy of the current subset
87
+ newSubset = list (currentSubset )
88
+
89
+ # Add the current element to the new subset
90
+ newSubset .append (ele )
91
+
92
+ # Append the new subset to the powerset
93
+ subset .append (newSubset )
94
+
95
+ # Return the powerset
96
+ return subset
You can’t perform that action at this time.
0 commit comments