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.
# Enter your code here. Read input from STDIN. Print output to STDOUTimportstatisticsasstatfromsysimportstdin,stdoutdefspearman(X,Y,n):defrank(arr):ref=sorted(arr)rank=[]foreleinarr:rank.append(ref.index(ele)+1)returnrankdefcorrelation(X,Y,n):std_x=stat.pstdev(X)std_y=stat.pstdev(Y)m_x=stat.mean(X)m_y=stat.mean(Y)corr=0foriinrange(n):corr+=(X[i]-m_x)*(Y[i]-m_y)/np_coeff=corr/(std_x*std_y)returnround(p_coeff,3)rank_x=rank(X)rank_y=rank(Y)spearman_coeff=correlation(rank_x,rank_y,n)stdout.write(str(spearman_coeff))n=int(stdin.readline().strip())X=list(map(float,stdin.readline().strip().split()))Y=list(map(float,stdin.readline().strip().split()))spearman(X,Y,n)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 7: Spearman's Rank Correlation Coefficient
You are viewing a single comment's thread. Return to all comments →