Created
December 1, 2014 01:38
-
-
Save Goodwine/5e153dc05467aa3df518 to your computer and use it in GitHub Desktop.
Gist Shows the *Proposed* implementation of how TopCoder assigns placement in a room.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = []; | |
n = 35000; | |
for (i = 0; i < n; i++) { | |
x.push({ | |
userName: Math.random(), | |
totalPoints: Math.random() * 100000, | |
}) | |
} | |
time = new Date().getTime(); | |
updateCoderPlacement = function (coders) { | |
// sort on descending order by totalPoints | |
coders.sort(function(a, b){ | |
// I don't think this is needed anymore: a.userName == b.userName | |
if (a.totalPoints > b.totalPoints) | |
return -1; | |
if (a.totalPoints < b.totalPoints) | |
return 1; | |
return 0; | |
}); | |
var roomPlace = 1; // coder's placement on the room. | |
var lastScore = -1; // to "freeze" roomPlace if the score is the same. | |
// assign the placement to each coder | |
coders.forEach(function(coder, index){ | |
if (coder.totalPoints != lastScore) { | |
roomPlace = index + 1; | |
lastScore = coder.totalPoints; | |
} | |
coder.roomPlace = roomPlace; | |
}); | |
/* we can revert to the original order here if that's required. */ | |
}; | |
updateCoderPlacement(x); | |
console.log('Total Time in updateCoderPlacement:', (new Date().getTime() - time) / 1000, 'seconds.'); | |
// Total Time in updateCoderPlacement: 0.025 seconds. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment