You are viewing a single comment's thread. Return to all comments →
function compareNumbers(a, b) { return a - b; } const data = input.split(/[\n]/g); const n = data[0], x = data[1].match(/\d+(?:\.\d+)?/g).map(Number), y= data[2].match(/\d+(?:\.\d+)?/g).map(Number); let sorted_x = [...x], sorted_y = [...y]; sorted_x.sort(compareNumbers); sorted_y.sort(compareNumbers); let d = 0; for(let i = 0; i < n; i++) { d += Math.pow((sorted_x.indexOf(x[i]) - sorted_y.indexOf(y[i])), 2); } let ans = 1 - ((6 * d) / (n * (Math.pow(n, 2) - 1))); console.log(ans.toFixed(3));
Day 7: Spearman's Rank Correlation Coefficient
You are viewing a single comment's thread. Return to all comments →
JavaScript