开发者

Generate standard competition rankings from a list of scores

Given a list of scores (e.g. 5, 5, 4, 2, 2, 0), I'm looking to return the standard competition rankings (1, 1, 3, 4, 4, 6).

From Wikipedia's ranking page, here's a summary of SRC:

Standard competition ranking ("1224" ranking)

In competition ranking, items that compare equal receive the same ranking number, and then a gap is left in the ranking numbers. The number of ranking numbers that are left out in this gap is one less than the number of items that compared equal. Equivalently, each item's ranking number is 1 plus the number of items rank开发者_运维技巧ed above it. This ranking strategy is frequently adopted for competitions, as it means that if two (or more) competitors tie for a position in the ranking, the position of all those ranked below them is unaffected (ie, a competitor only comes second if exactly one person scores better than them, third if exactly two people score better than them, fourth if exactly three people score better than them, etc).

Thus if A ranks ahead of B and C (which compare equal) which are both ranked ahead of D, then A gets ranking number 1 ("first"), B gets ranking number 2 ("joint second"), C also gets ranking number 2 ("joint second") and D gets ranking number 4 ("fourth"). In this case, nobody would get ranking number 3 ("third") and that would be left as a gap.

Code in any language would be really helpful.

As an aside I'd be interested to see algorithms for the other ranking types (modified competition ranking, dense ranking, ordinal ranking and fractional ranking).


Here's how to do it:

  • Set ranking[0] to 1.
  • For each index i in the score-list
    • If score[i] equals score[i-1] they should have same ranknig:
      • ranking[i] = ranknig[i-1]
    • else the ranking should equal the current index:
      • ranking[i] = i + 1
        (+ 1 due to 0-based indecies and 1-based ranking)

Here's a sample implementation in Java:

// Set up some sample scores.
int[] scores = { 5, 5, 4, 2, 2, 0 };

// Initialize a ranking array
int[] rankings = new int[scores.length];

// Fill in each position
rankings[0] = 1;
for (int i = 1; i < rankings.length; i++)
    rankings[i] = scores[i] == scores[i-1] ? rankings[i-1] : i + 1;

// rankings = [1, 1, 3, 4, 4, 6]


Here is some php code to do it:

$scores = array(5,5,4,2,2,0);

for($i = 0, $j = 0,
  $stop = count($scores),
  $last_score = -1; $i < $stop; $i++){
    if($scores[$i] != $last_score){
        $j = $i+1;
        $last_score = $scores[$i];
    }
    $scores[$i] = $j;   
}

// Now scores = [1,1,3,4,4,6]

If you know that your scores are in order already you can remove the rsort

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜