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
def binaryToDecimal ( n ) : NEW_LINE INDENT num = n NEW_LINE dec_value = 0 NEW_LINE base = 1 NEW_LINE length = len ( num ) NEW_LINE for i in range ( length - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( num [ i ] == '1' ) : NEW_LINE INDENT dec_value += base NEW_LINE DEDENT base = base * 2 NEW_LINE DEDENT return dec_value NEW_LINE DEDENT def decimalToBinary ( n ) : NEW_LINE INDENT binstr = " " NEW_LINE while ( n > 0 ) : NEW_LINE INDENT binstr += chr ( n % 2 + 48 ) NEW_LINE n = n // 2 NEW_LINE DEDENT return binstr NEW_LINE DEDENT def reversedBinaryDecimal ( N ) : NEW_LINE INDENT decimal_to_binar = decimalToBinary ( N ) NEW_LINE binary_to_decimal = binaryToDecimal ( decimal_to_binar ) NEW_LINE return binary_to_decimal NEW_LINE DEDENT def printSortedArray ( arr , size ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def modifyArray ( arr , size ) : NEW_LINE INDENT for i in range ( size ) : NEW_LINE INDENT arr [ i ] = reversedBinaryDecimal ( arr [ i ] ) NEW_LINE DEDENT printSortedArray ( arr , size ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 98 , 43 , 66 , 83 ] NEW_LINE n = len ( arr ) NEW_LINE modifyArray ( arr , n ) NEW_LINE DEDENT