Sort by

recency

|

422 Discussions

|

  • + 0 comments
    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 interQuartile(values, freqs):
        expanded_data = []
        for i in range(len(values)):
            expanded_data.extend([values[i]] * freqs[i])
        expanded_data.sort()
        q2 = find_median(expanded_data)
        n = len(expanded_data)
        if n % 2 == 1:
            lower_half = expanded_data[:n // 2]  
            upper_half = expanded_data[n // 2 + 1:]  
        else:
            lower_half = expanded_data[:n // 2]  
            upper_half = expanded_data[n // 2:]  
        q1 = find_median(lower_half)
        q3 = find_median(upper_half)
        iqr = q3 - q1
        print(f"{iqr:.1f}")
    
  • + 0 comments
    S = sorted([v for v,f in zip(values,freqs) for _ in range(f)])
    L = len(S)
    Q1 = (S[(L//4)-1] + S[(L//4)]) /2 if L%4 == 0 else S[L//4]
    Q3 = (S[3*(L//4)-1] + S[3*(L//4)]) /2 if 3*L%4 == 0 else S[3*L//4]
    print( f"{Q3 - Q1:.1f}")
    
  • [deleted]
    + 0 comments
    def interQuartile(values, freqs):
        # Print your answer to 1 decimal place within this function
        arr=[]
        for i in range(len(values)):
            ans = [values[i]] * freqs[i]  
            arr.extend(ans)  
        arr.sort()
        
        n = len(arr)
        mid = n//2
        
        if n%2 == 0:
            L = arr[:mid]
            U = arr[mid:]
        else:
            L = arr[:mid]
            U = arr[mid+1:]
        
        Q1 = median(L)
        Q3 = median(U)
        
        answer = Q3-Q1
        print(f"{answer:.1f}")
        
    def median(a):
        n = len(a)
        mid = n//2
        if n % 2 == 0:
            median = (a[mid] + a[~mid]) / 2
        else:
            median = a[mid]
            
        return round(median)
    
  • + 0 comments

    def interQuartile(values, freqs): # Create the data set S S = [] for i in range(len(values)): S += [values[i]] * freqs[i]

    S = sorted(S)
    n = len(S)
    
    def median(sub_arr):
        sub_n = len(sub_arr)
        mid = sub_n // 2
        if sub_n % 2 == 0:
            return (sub_arr[mid - 1] + sub_arr[mid]) / 2.0
        else:
            return sub_arr[mid]
    
    # Calculate Q1
    lower_half = S[:n // 2]
    Q1 = median(lower_half)
    
    # Calculate Q3
    if n % 2 == 0:
        upper_half = S[n // 2:]
    else:
        upper_half = S[(n // 2) + 1:]
    Q3 = median(upper_half)
    
    # Print the interquartile range to 1 decimal place
    print(f"{Q3 - Q1:.1f}")
    

    if name == 'main': n = int(input().strip()) values = list(map(int, input().rstrip().split())) freqs = list(map(int, input().rstrip().split()))

    # Ensure that the number of elements in S is equal to the sum of freqs
    assert len(values) == len(freqs), "The length of values and freqs must be the same"
    assert sum(freqs) <= 1000, "The sum of frequencies must be less than or equal to 1000"
    
    interQuartile(values, freqs)
    
  • + 0 comments

    python 3

    def interQuartile(values, freqs):
        l=[]
        for i in range(len(values)):
            for j in range(freqs[i]):
                l.append(values[i])
        l=sorted(l)
        if len(l)%2==0:
            print(float(statistics.median(l[len(l)//2:])-statistics.median(l[:len(l)//2])))
        else:
            print(float(statistics.median(l[(len(l)//2)+1:])-statistics.median(l[:len(l)//2])))