Replace each element of Array with it 's corresponding rank | Function to assign rank to array elements ; Copy input array into newArray ; Sort newArray [ ] in ascending order ; Dictionary to store the rank of the array element ; Update rank of element ; Assign ranks to elements ; Driver Code ; Given array arr [ ] ; Function call ; Print the array elements
def changeArr ( input1 ) : NEW_LINE INDENT newArray = input1 . copy ( ) NEW_LINE newArray . sort ( ) NEW_LINE ranks = { } NEW_LINE rank = 1 NEW_LINE for index in range ( len ( newArray ) ) : NEW_LINE INDENT element = newArray [ index ] ; NEW_LINE if element not in ranks : NEW_LINE INDENT ranks [ element ] = rank NEW_LINE rank += 1 NEW_LINE DEDENT DEDENT for index in range ( len ( input1 ) ) : NEW_LINE INDENT element = input1 [ index ] NEW_LINE input1 [ index ] = ranks [ input1 [ index ] ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 100 , 2 , 70 , 2 ] NEW_LINE changeArr ( arr ) NEW_LINE print ( arr ) NEW_LINE DEDENT