Last active
February 8, 2017 15:42
-
-
Save jonarddoci/d1df0fd8648124592388c8d3a0a90ed6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function calc(){ | |
var players = [ | |
{ name: 'arif', score: 5 }, | |
{ name: 'alp', score: 8 }, | |
{ name: 'bahar', score: 6 }, | |
{ name: 'selcuk', score: 3 }, | |
{ name: 'ibrahim', score: 4 }, | |
{ name: 'caner', score: 3 }, | |
{ name: 'cagatay', score: 9 }, | |
{ name: 'furkan', score: 6 }, | |
{ name: 'jonard', score: 10 }, | |
{ name: 'kadir', score: 7 }, | |
{ name: 'okaym', score: 6 }, | |
{ name: 'sezgin', score: 6 }, | |
{ name: 'zafer', score: 7 }, | |
{ name: 'serkan', score: 5 }, | |
{ name: 'ufuk', score: 2 }, | |
{ name: 'ozgur', score: 5 }, | |
{ name: 'tarik', score: 9 }, | |
{ name: 'tolga', score: 7 }]; | |
players.sort(function(a,b){ | |
if(a.score == b.score){ | |
return .5 - Math.random(); | |
} | |
return (b.score - a.score) | |
}); | |
//players.sort(function(a,b){ return .5 - Math.random();}) | |
var partitioned = []; | |
var numPartitions = 4; | |
for(var i=0;i<numPartitions; i++){ | |
partitioned.push({ | |
totalWeight:0, | |
players:[] | |
}); | |
} | |
function findMin(partitions){ | |
var minIndex=0; | |
for(var i=0; i< partitions.length; i++){ | |
if(partitions[i].players.length >= numPartitions){ | |
continue; | |
} | |
if(partitions[i].totalWeight < partitions[minIndex].totalWeight){ | |
minIndex=i; | |
} | |
} | |
if(minIndex==0 && partitions[0].length>=numPartitions){ | |
for(var i=1; i< partitions.length; i++){ | |
if(partitions[i].players.length < numPartitions){ | |
minIndex = i; | |
break; | |
} | |
} | |
} | |
return minIndex; | |
} | |
function putPlayer(player){ | |
var partitionIndex = findMin(partitioned); | |
partitioned[partitionIndex].players.push(player); | |
partitioned[partitionIndex].totalWeight+=player.score; | |
} | |
for(var i=0; i< players.length; i++){ | |
putPlayer(players[i]); | |
} | |
for(var i=0;i<numPartitions; i++){ | |
console.log("Team "+(i+1)) | |
partitioned[i].players.sort(function(a,b){ return .5 - Math.random();}) | |
for(var j =0; j<partitioned[i].players.length; j++){ | |
console.log(partitioned[i].players[j].name + ": "+partitioned[i].players[j].score); | |
} | |
console.log("Estimated score: "+partitioned[i].totalWeight) | |
console.log("-----"); | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment