human
Write me a code to modify an array by sorting after reversal of bits of each array element and the other steps required given the following:
Modify an array by sorting after reversal of bits of each array element | Function to convert binary number to equivalent decimal.
Set base value to 1 , i . e 2 ^ 0.
Function to convert a decimal to equivalent binary representation.
Stores the binary representation.
Since ASCII value of '0' , '1' are 48 and 49.
As the string is already reversed , no further reversal is required.
Function to convert the reversed binary representation to equivalent integer.
Stores reversed binary representation of given decimal.
Stores equivalent decimal value of the binary representation.
Return the resultant integer.
Utility function to print the sorted array.
Sort the array.
Traverse the array.
Print the array elements.
Utility function to reverse the binary representations of all array elements and sort the modified array.
Traverse the array.
Passing array elements to reversedBinaryDecimal function.
Pass the array to the sorted array.
Driver Code"
Solution in Python:
assistant
#Here is code to modify an array by sorting after reversal of bits of each array element and the other steps required.
 def binaryToDecimal ( n ) :
	num = n
	dec_value = 0
	base = 1
	length = len ( num )
	for i in range ( length - 1 , - 1 , - 1 ) :
		if ( num [ i ] == '1' ) :
			dec_value += base
		base = base * 2
	return dec_value
def decimalToBinary ( n ) :
	binstr = " "
	while ( n > 0 ) :
		binstr += chr ( n % 2 + 48 )
		n = n // 2
	return binstr
def reversedBinaryDecimal ( N ) :
	decimal_to_binar = decimalToBinary ( N )
	binary_to_decimal = binaryToDecimal ( decimal_to_binar )
	return binary_to_decimal
def printSortedArray ( arr , size ) :
	arr . sort ( )
	for i in range ( size ) :
		print ( arr [ i ] , end = " ▁ " )
	print ( )
def modifyArray ( arr , size ) :
	for i in range ( size ) :
		arr [ i ] = reversedBinaryDecimal ( arr [ i ] )
	printSortedArray ( arr , size )
if __name__ == " _ _ main _ _ " :
	arr = [ 98 , 43 , 66 , 83 ]
	n = len ( arr )
	modifyArray ( arr , n )