human
Find all distinct subsets of a given set using BitMasking Approach | Function to find all subsets of given set . Any repeated subset is considered only once in the output.
Run counter i from 000. . 0 to 111. . 1.
consider each element in the set.
Check if jth bit in the i is set . If the bit is set , we consider jth element from set.
if subset is encountered for the first time If we use set < string > , we can directly insert.
consider every subset.
split the subset and print its elements.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to find all distinct subsets of a given set using BitMasking Approach and the other steps required.
 def printPowerSet ( arr , n ) :
	_list = [ ]
	for i in range ( 2 ** n ) :
		subset = " "
		for j in range ( n ) :
			if ( i & ( 1 << j ) ) != 0 :
				subset += str ( arr [ j ] ) + " | "
		if subset not in _list and len ( subset ) > 0 :
			_list . append ( subset )
	for subset in _list :
		arr = subset . split ( ' ▁ ' )
		for string in arr :
			print ( string , end = " ▁ " )
		print ( )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 10 , 12 , 12 ]
	n = len ( arr )
	printPowerSet ( arr , n )