Sort by

recency

|

505 Discussions

|

  • [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))
    
  • + 0 comments

    This test took me few hours to solve. I learned some techniques to solve quartiles problems but they don't work. Finally I could solve this way:

    def get_quartile_position(number: float, is_even: float = False) -> [int, tuple]:
        if is_even:
            number = int(number)
            return number, number + 1
        return round(number)
    
    
    def get_positions(total_population: int) -> tuple[any, any, any]:
        even_population = total_population % 2 == 0
        has_even_quartiles = (total_population // 2) % 2 == 0
        total_population += 1
        q1_position = total_population / 4
        q1_position = get_quartile_position(q1_position, has_even_quartiles)
        q2_position = total_population / 2
        q2_position = get_quartile_position(q2_position, even_population)
        q3_position = (3 * total_population) / 4
        q3_position = get_quartile_position(q3_position, has_even_quartiles)
    
        return q1_position, q2_position, q3_position
    
    
    def get_quartiles_values(group: dict, pos: [int, tuple]) -> int:
        if type(pos) is tuple:
            return round((group[pos[0]] + group[pos[1]]) / 2)
    
        return group[pos]
    
    
    def quartiles(arr) -> list[int]:
        arr.sort()
        n = len(arr)
        arr = dict(enumerate(arr, start=1))
        return list(map(lambda pos: get_quartiles_values(arr, pos), get_positions(n)))