Rank of all elements in an array | Python Code to find rank of elements ; Rank Vector ; Sweep through all elements in A for each element count the number of less than and equal elements separately in r and s . ; Use formula to obtain rank ; Driver code
def rankify ( A ) : NEW_LINE INDENT R = [ 0 for x in range ( len ( A ) ) ] NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT ( r , s ) = ( 1 , 1 ) NEW_LINE for j in range ( len ( A ) ) : NEW_LINE INDENT if j != i and A [ j ] < A [ i ] : NEW_LINE INDENT r += 1 NEW_LINE DEDENT if j != i and A [ j ] == A [ i ] : NEW_LINE INDENT s += 1 NEW_LINE DEDENT DEDENT R [ i ] = r + ( s - 1 ) / 2 NEW_LINE DEDENT return R NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 5 , 2 , 1 , 25 , 2 ] NEW_LINE print ( A ) NEW_LINE print ( rankify ( A ) ) NEW_LINE DEDENT