human
Sort an array according to absolute difference with given value | Function to sort an array according absolute difference with x ..
Store values in a map with the difference with X as key.
Update the values of array.
Function to print the array.
Driver code"
How can the above be solved in Python?
 ... Give me a code to sort an array according to absolute difference with given value and the other steps required?
assistant
#Here is code to sort an array according to absolute difference with given value and the other steps required.
 def rearrange ( arr , n , x ) :
	m = { }
	for i in range ( n ) :
		m [ arr [ i ] ] = abs ( x - arr [ i ] )
	m = { k : v for k , v in sorted ( m . items ( ) , key = lambda item : item [ 1 ] ) }
	i = 0
	for it in m . keys ( ) :
		arr [ i ] = it
		i += 1
def printArray ( arr , n ) :
	for i in range ( n ) :
		print ( arr [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
	arr = [ 10 , 5 , 3 , 9 , 2 ]
	n = len ( arr )
	x = 7
	rearrange ( arr , n , x )
	printArray ( arr , n )