Interquartile Range ( IQR ) | Function to give index of the median ; Function to calculate IQR ; Index of median of entire data ; Median of first half ; Median of second half ; IQR calculation ; Driver Function
def median ( a , l , r ) : NEW_LINE INDENT n = r - l + 1 NEW_LINE n = ( n + 1 ) // 2 - 1 NEW_LINE return n + l NEW_LINE DEDENT def IQR ( a , n ) : NEW_LINE INDENT a . sort ( ) NEW_LINE mid_index = median ( a , 0 , n ) NEW_LINE Q1 = a [ median ( a , 0 , mid_index ) ] NEW_LINE Q3 = a [ mid_index + median ( a , mid_index + 1 , n ) ] NEW_LINE return ( Q3 - Q1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 19 , 7 , 6 , 5 , 9 , 12 , 27 , 18 , 2 , 15 ] NEW_LINE n = len ( a ) NEW_LINE print ( IQR ( a , n ) ) NEW_LINE DEDENT