human
Sort an array according to count of set bits | Function to count setbits.
Function to sort By SetBitCount.
Iterate over all values and insert into multimap.
Driver Code"
How can the above be solved in Python?
 Write me a code to sort an array according to count of set bits and the other steps required?
assistant
#Here is code to sort an array according to count of set bits and the other steps required.
 def setBitCount ( num ) :
	count = 0
	while ( num ) :
		if ( num & 1 ) :
			count += 1
		num = num >> 1
	return count
def sortBySetBitCount ( arr , n ) :
	count = [ ]
	for i in range ( n ) :
		count . append ( [ ( - 1 ) * setBitCount ( arr [ i ] ) , arr [ i ] ] )
	count . sort ( key = lambda x : x [ 0 ] )
	for i in range ( len ( count ) ) :
		print ( count [ i ] [ 1 ] , end = " ▁ " )
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
n = len ( arr )
sortBySetBitCount ( arr , n )