Sort by

recency

|

506 Discussions

|

  • + 0 comments
    def quartiles(n,arr):
        arr=sorted(arr)
        if n%2==1:
            q2=arr[n//2]
            q1=statistics.median(arr[:n//2])
            q3=statistics.median(arr[n//2+1:])
        else:
            q2=(arr[n//2]+arr[n//2-1])/2
            q1=statistics.median(arr[:n//2])
            q3=statistics.median(arr[n//2:])
        return [int(q1),int(q2),int(q3)]
    

    or

    def find_median(arr):
        n = len(arr)
        if n % 2 == 1:
            return arr[n // 2]
        else:
            return (arr[n // 2] + arr[n // 2 - 1]) / 2
    
    def quartiles(n, arr):
        arr = sorted(arr)
        q2 = find_median(arr)
        if n % 2 == 1:
            lower_half = arr[:n // 2]
            upper_half = arr[n // 2 + 1:]
        else:
            lower_half = arr[:n // 2]
            upper_half = arr[n // 2:]
        q1 = find_median(lower_half)
        q3 = find_median(upper_half)
        return [int(q1), int(q2), int(q3)]
    

    undefined `

  • [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)
    		
    
  • + 0 comments
    def median(data):
        num = len(data)
        if num % 2 == 0:
            return (data[num//2 - 1] + data[num//2]) / 2
        
        return data[(num-1) // 2]
    
    def quartiles(arr):
        n = len(arr)
        arrX = sorted(arr)
        Q2 = median(arrX)
        
        if n % 2 != 0:
            Q1 = median(arrX[:(n-1)//2])
            Q3 = median(arrX[(n+1)//2:])
        else:
            Q1 = median(arrX[:(n//2)])
            Q3 = median(arrX[(n//2):])
        
        return int(Q1), int(Q2), int(Q3)
    
  • + 0 comments
    def quartiles(arr):
        k=[]
        arr=sorted(arr)
        l=arr[:len(arr)//2]
        if len(arr)%2!=0:
            e=arr[(len(arr)//2)+1:]
        else:
             e=arr[(len(arr)//2):]
        k.append(int(statistics.median(l)))
        k.append(int(statistics.median(arr)))
        k.append(int(statistics.median(e)))
        return k
    
  • + 0 comments
    arr=sorted(arr)
        if n%2==0:
            l=[arr[i] for i in range(round(n/2))]
            u=[arr[i] for i in range(round(n/2),len(arr))]
            return int(statistics.median(l)),int(statistics.median(arr)),int(statistics.median(u))
        else:
            l=[arr[i] for i in range(round(n/2))]
            u=[arr[i] for i in range(round(n/2)+1,len(arr))]
            return int(statistics.median(l)),int(statistics.median(arr)),int(statistics.median(u))