You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
648 B
31 lines
648 B
/**
|
|
* A few useful function for dealing with majority judgment
|
|
*/
|
|
|
|
|
|
/**
|
|
* Return the index corresponding to the majority grade
|
|
*/
|
|
export const getMajorityGrade = (votes: Array<number>): number => {
|
|
const indices = votes.map((_, i) => i);
|
|
const numVotes = votes.reduce((a, b) => a + b, 0)
|
|
|
|
let majorityGrade = indices[0]
|
|
let accBefore = 0
|
|
let isBefore = true
|
|
|
|
for (const gradeId in votes) {
|
|
if (isBefore) {
|
|
accBefore += votes[gradeId]
|
|
}
|
|
if (isBefore && accBefore > numVotes / 2) {
|
|
majorityGrade = indices[gradeId]
|
|
accBefore -= votes[gradeId]
|
|
isBefore = false
|
|
}
|
|
}
|
|
|
|
return majorityGrade;
|
|
}
|
|
|