Sort by

recency

|

421 Discussions

|

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

    I can't pass test 5 and 6 can somebody help me plz

    def interQuartile(values, freqs):
        # Print your answer to 1 decimal place within this function
        new_lst = []
        for i in range(n):
            temp_freq = freqs[i]
            for j in range(temp_freq-1):
                val.append(val[i])
        new_lst = sorted(val)
        #print(new_lst)
        med = statistics.median(new_lst)
        print(med)
        Q1 =[]
        Q3 =[]
        for i in range(len(new_lst)):
            if new_lst[i] < med:
                Q1.append(new_lst[i])
            elif new_lst[i] > med:
                Q3.append(new_lst[i])
        Q1_med = statistics.median(Q1)
        Q3_med = statistics.median(Q3)
        print(Q1_med, Q3_med)
        return round(Q3_med - Q1_med, 1)