I'm developing a system where students are added to color groups (RED,BLUE,GREEN,YELLOW). They need to be added evenly. Here is a picture of the logic for adding a student to a group
Here is the logic I have so far (purely based on number of students in each group):
var studentsByColorGroup={"GREEN":[], "BLUE":[], "RED":[], "YELLOW":[]};
var gradeCountsByColorGroup={
"GREEN" :{3:0, 4:0, 5:0, 6:0},
"BLUE" :{3:0, 4:0, 5:0, 6:0},
"RED" :{3:0, 4:0, 5:0, 6:0},
"YELLOW":{3:0, 4:0, 5:0, 6:0}
};
for(var student of students) {
studentsByColorGroup[student.color].push(student);
gradeCountsByColorGroup[student.color][student.grade]++;
}
var greenCount =studentsByColorGroup["GREEN" ].length;
var blueCount =studentsByColorGroup["BLUE" ].length;
var redCount =studentsByColorGroup["RED" ].length;
var yellowCount=studentsByColorGroup["YELLOW"].length;
var list={
"GREEN" :this.greenCount,
"BLUE" :this.blueCount,
"RED" :this.redCount,
"YELLOW":this.yellowCount
};
var keysSorted:string[]=Object.keys(list).sort((a, b) => list[a]-list[b]);
// The first element in the list of sorted keys should
// be the color with the LEAST students in it
newStudent.color=keysSorted[0];
Now, I need to add the part that makes sure that students are balanced as much as possible with respect to grade. Please see the image for details.
How would you alter the above algorithm to programmatically determine the new student's color group?
By the way, this is a real-world problem (not just for student algorithm assignment) for a system I'm developing which goes live next Wednesday