• [deleted]
    + 0 comments
    def quartiles(arr):
        # Sort the array
        arr.sort()
        
        n = len(arr)
        mid = n // 2
        
        # Calculate Q1 (first quartile)
        if n % 2 == 0:
            Q1 = quants(arr[:mid])
        if n % 2 != 0:
            Q1 = quants(arr[:mid])
        
        # Calculate Q2 (median)
        Q2 = quants(arr)
        
        # Calculate Q3 (third quartile)
        if n % 2 == 0:
            Q3 = quants(arr[mid:])
        else:
            Q3 = quants(arr[mid+1:])
        
        return [Q1, Q2, Q3]
    
    def quants(a):
        n = len(a)
        mid = n // 2
        
        if n % 2 != 0:
            median = a[mid]
        else:
            median = (a[mid] + a[~mid]) / 2
            
        return round(median)