20
20
# subset.pop_back();
21
21
# }
22
22
# }
23
+ def generate_subsets (nums : list [int ]) -> list [list [int ]]:
24
+ """
25
+ Explanation of the Code
26
+ Initialization:
27
+
28
+ generate_subsets(nums): Initializes an empty list result to store all subsets and calls the backtrack function starting from the first index.
29
+ Backtracking Function:
30
+
31
+ backtrack(start, current): A recursive function that generates all subsets starting from index start and using the current subset current.
32
+ Base Case: The base case is implicitly handled by the loop termination. When start equals the length of nums, there are no more elements to consider.
33
+ Recursive Step:
34
+
35
+ For each element starting from start to the end of the list nums, the function explores two possibilities:
36
+ Include: Add nums[i] to the current subset current and call backtrack recursively with the next index (i + 1).
37
+ Exclude: Remove nums[i] from current (backtrack) and proceed to the next iteration.
38
+ Adding Subsets:
39
+
40
+ Every time backtrack is called, the current subset current is added to the result list result. This ensures that all subsets are captured.
41
+ Example Usage
42
+ For nums = [1, 2, 3], the function generates the following subsets:
43
+
44
+ Start with an empty subset: []
45
+ Include 1: [1]
46
+ Include 2: [1, 2]
47
+ Include 3: [1, 2, 3]
48
+ Backtrack to exclude 3: [1, 2]
49
+ Backtrack to exclude 2 and include 3: [1, 3]
50
+ Backtrack to exclude 1 and include 2: [2]
51
+ Include 3: [2, 3]
52
+ Backtrack to exclude 3: [2]
53
+ Backtrack to exclude 2 and include 3: [3]
54
+ Backtrack to exclude 3: []"""
55
+ def backtrack (start : int , current : list [int ]) -> None :
56
+ # Add the current subset to the result list
57
+ result .append (current [:])
58
+
59
+ for i in range (start , len (nums )):
60
+ # Include nums[i] in the current subset
61
+ current .append (nums [i ])
62
+ # Move to the next element
63
+ backtrack (i + 1 , current )
64
+ # Exclude nums[i] from the current subset (backtrack)
65
+ current .pop ()
66
+
67
+ result : list [list [int ]] = []
68
+ backtrack (0 , [])
69
+ return result
70
+
23
71
24
72
def search (k : int , n : int , subset : list [Any ], all_subsets : list [list [Any ]]):
25
73
"""
@@ -45,12 +93,14 @@ def search(k: int, n: int, subset: list[Any], all_subsets: list[list[Any]]):
45
93
# Include nums[k] in the subset
46
94
# The element k is then added to the subset, and search(k + 1) is called again to include the current element in the subset.
47
95
subset .append (k )
96
+ print (f"included { k } index in subset: { subset } " )
48
97
search (k + 1 , n , subset , all_subsets )
49
98
50
99
subset .pop ()
100
+ print (f"removed { k } index from subset: { subset } " )
51
101
52
102
def subsets_recursive (nums : List [int ]) -> List [List [int ]]:
53
- all_subsets = []
103
+ all_subsets : list [ list [ int ]] = []
54
104
search (0 , len (nums ), [], all_subsets )
55
105
return all_subsets
56
106
@@ -66,13 +116,67 @@ def subsets_recursive(nums: List[int]) -> List[List[int]]:
66
116
# return result
67
117
68
118
69
- # def subsets_bitmask(nums: List[int]) -> List[List[int]]:
70
- # n = len(nums)
71
- # result = []
72
- # for i in range(1 << n):
73
- # subset = []
74
- # for j in range(n):
75
- # if i & (1 << j):
76
- # subset.append(nums[j])
77
- # result.append(subset)
78
- # return result
119
+ def generate_subsets (n : int ) -> list [list [int ]]:
120
+ """
121
+ Explanation of the Python Code
122
+ Outer Loop (for b in range(1 << n)):
123
+
124
+ The variable b ranges from 0 to 2^n - 1.
125
+ 1 << n is equivalent to 2^n, so this loop iterates 2^n
126
+ times, generating all possible subsets.
127
+
128
+ Inner Loop (for i in range(n)):
129
+
130
+ For each value of b, this loop checks each bit position i from 0 to n-1.
131
+ Bit Check (if b & (1 << i)):
132
+
133
+ 1 << i creates a number with only the i-th bit set.
134
+ b & (1 << i) checks if the i-th bit in b is set.
135
+ If the i-th bit is set, it means the i-th element is included in the current subset.
136
+ Subset Construction (subset.append(i)):
137
+
138
+ If the bit check is true, the i-th element is added to the current subset.
139
+ Collecting All Subsets (all_subsets.append(subset)):
140
+
141
+ Each generated subset is added to the list all_subsets."""
142
+ all_subsets : list [list [int ]] = []
143
+ for b in range (1 << n ): # Iterate over all possible subsets
144
+ subset : list [int ] = []
145
+ for i in range (n ): # Check each bit position
146
+ if b & (1 << i ): # If the ith bit is set, include i in the subset
147
+ subset .append (i )
148
+ all_subsets .append (subset )
149
+ return all_subsets
150
+
151
+ # Example usage
152
+ n = 3
153
+ subsets = generate_subsets (n )
154
+ print (subsets ) # Output: [[], [0], [1], [0, 1], [2], [0, 2], [1, 2], [0, 1, 2]]
155
+
156
+ def generate_subsets (nums : list [Any ]) -> list [list [Any ]]:
157
+ n = len (nums )
158
+ all_subsets : list [list [Any ]] = []
159
+
160
+ for b in range (1 << n ): # Iterate over all possible subsets (2^n subsets)
161
+ subset = [nums [i ] for i in range (n ) if b & (1 << i )] # Use list comprehension to build the subset
162
+ all_subsets .append (subset )
163
+
164
+ return all_subsets
165
+
166
+ # Example usage
167
+ nums = [1 , 2 , 3 ]
168
+ subsets = generate_subsets (nums )
169
+ print (subsets )
170
+
171
+ def binary_and (str1 : str , str2 : str ) -> str :
172
+ # Remove '0b' prefix and ensure equal lengths
173
+ str1 = str1 [2 :].zfill (max (len (str1 ), len (str2 )) - 2 )
174
+ str2 = str2 [2 :].zfill (max (len (str1 ), len (str2 )) - 2 )
175
+
176
+ result = ""
177
+ for i in range (len (str1 )):
178
+ result += '1' if str1 [i ] == '1' and str2 [i ] == '1' else '0'
179
+ return '0b' + result
180
+
181
+ combined_binary = binary_and (bin (1 ), bin (1 << 2 ))
182
+ print (combined_binary ) # Output: 0b0
0 commit comments