File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -66,3 +66,31 @@ def powerset(array):
6666def Powerset (array ):
6767 # Call the helper function to generate the powerset starting from the last index
6868 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