Skip to content

Instantly share code, notes, and snippets.

@Goodwine
Created December 1, 2014 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Goodwine/5e153dc05467aa3df518 to your computer and use it in GitHub Desktop.
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.
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