We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
fromitertoolsimportrepeat## Complete the 'interQuartile' function below.## The function accepts following parameters:# 1. INTEGER_ARRAY values# 2. INTEGER_ARRAY freqs#defcreate_array(array,freqs):new_array=[]foriinrange(len(freqs)):new_array.extend(repeat(array[i],freqs[i]))returnnew_arraydefgetq2(array):#this removes q2 from the dataset since we don't want it in either halves when the dataset is split(only in odd datsets)array.pop(round(len(array)/2))returnarraydefgetmedian(array):median=Noneiflen(array)%2==0:first_item=array[int(len(array)/2)]second_item=array[int(len(array)/2)-1]median=(first_item+second_item)//2else:median=array[int(len(array)/2)]returnmediandeflower_upper_half(array):lower_half=array[:round(len(array)/2)]upper_half=array[round(len(array)/2):]return(lower_half,upper_half)definterQuartile(values,freqs):# Print your answer to 1 decimal place within this functioncreated_array=create_array(values,freqs)created_array.sort()lower_half,upper_half=[],[]iflen(created_array)%2==0:#even datasetlower_half,upper_half=lower_upper_half(created_array)else:#odd median outnew_array=getq2(created_array)lower_half,upper_half=lower_upper_half(new_array)list(lower_half)list(upper_half)q1=getmedian(lower_half)q3=getmedian(upper_half)iqr=(q3-q1)print(float(iqr))if__name__=='__main__':n=int(input().strip())val=list(map(int,input().rstrip().split()))freq=list(map(int,input().rstrip().split()))interQuartile(val,freq)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 1: Interquartile Range
You are viewing a single comment's thread. Return to all comments →