单独使用 PHP 的完美排名
Perfect Ranking with ties using PHP alone
在我试图找到带领带的完美排名解决方案时,我一直无法给出正确的排名。代码将解释我想要更好地实现的目标:
我有以下数组:
$standings = array(
'player_1' => 30,
'player_2' => 26,
'player_3' => 28,
'player_9' => 28
);
现在为了对它进行并列排名,我有以下功能:
function setRanking($standings) {
$rankings = array();
arsort($standings);
$rank = 1;
$tie_rank = 0;
$prev_score = -1;
foreach ($standings as $name => $score) {
if ($score != $prev_score) { //this score is not a tie
$count = 0;
$prev_score = $score;
$rankings[$name] = array('score' => $score, 'rank' => $rank);
} else { //this score is a tie
$prev_score = $score;
if ($count++ == 0) {
$tie_rank = $rank - 1;
}
$rankings[$name] = array('score' => $score, 'rank' => $tie_rank);
}
$rank++;
}
return $rankings;
}
echo '<pre>';
print_r(setRanking($standings));
输出:
Array
(
[player_1] => Array
(
[score] => 30
[rank] => 1
)
[player_3] => Array
(
[score] => 28
[rank] => 2
)
[player_9] => Array
(
[score] => 28
[rank] => 2
)
[player_2] => Array
(
[score] => 26
[rank] => 4
)
)
预期输出:
Array
(
[player_1] => Array
(
[score] => 30
[rank] => 1
)
[player_3] => Array
(
[score] => 28
[rank] => 2
)
[player_9] => Array
(
[score] => 28
[rank] => 2
)
[player_2] => Array
(
[score] => 26
[rank] => 3
)
)
问题是它将排名打印为 1,2,2,4
,而根据我的假设它应该打印 1,2,2,3
。那么它如何打印 1,2,2,3 呢?哪里可能出错?任何帮助表示赞赏。谢谢。
只需更改您的 else 块,您不会递减 $rank
值,因此它保持与现在相同的值。您只需执行 $rank - 1
即可将其减去 1 存储在变量中而不更改主值。
foreach ($standings as $name => $score) {
if ($score != $prev_score) { //this score is not a tie
$count = 0;
$prev_score = $score;
$rankings[$name] = array('score' => $score, 'rank' => $rank);
} else { //this score is a tie
$prev_score = $score;
$rank--; // Decrements here
if ($count++ == 0) {
$tie_rank = $rank;
}
$rankings[$name] = array('score' => $score, 'rank' => $tie_rank);
}
$rank++;
}
return $rankings;
在我试图找到带领带的完美排名解决方案时,我一直无法给出正确的排名。代码将解释我想要更好地实现的目标:
我有以下数组:
$standings = array(
'player_1' => 30,
'player_2' => 26,
'player_3' => 28,
'player_9' => 28
);
现在为了对它进行并列排名,我有以下功能:
function setRanking($standings) {
$rankings = array();
arsort($standings);
$rank = 1;
$tie_rank = 0;
$prev_score = -1;
foreach ($standings as $name => $score) {
if ($score != $prev_score) { //this score is not a tie
$count = 0;
$prev_score = $score;
$rankings[$name] = array('score' => $score, 'rank' => $rank);
} else { //this score is a tie
$prev_score = $score;
if ($count++ == 0) {
$tie_rank = $rank - 1;
}
$rankings[$name] = array('score' => $score, 'rank' => $tie_rank);
}
$rank++;
}
return $rankings;
}
echo '<pre>';
print_r(setRanking($standings));
输出:
Array
(
[player_1] => Array
(
[score] => 30
[rank] => 1
)
[player_3] => Array
(
[score] => 28
[rank] => 2
)
[player_9] => Array
(
[score] => 28
[rank] => 2
)
[player_2] => Array
(
[score] => 26
[rank] => 4
)
)
预期输出:
Array
(
[player_1] => Array
(
[score] => 30
[rank] => 1
)
[player_3] => Array
(
[score] => 28
[rank] => 2
)
[player_9] => Array
(
[score] => 28
[rank] => 2
)
[player_2] => Array
(
[score] => 26
[rank] => 3
)
)
问题是它将排名打印为 1,2,2,4
,而根据我的假设它应该打印 1,2,2,3
。那么它如何打印 1,2,2,3 呢?哪里可能出错?任何帮助表示赞赏。谢谢。
只需更改您的 else 块,您不会递减 $rank
值,因此它保持与现在相同的值。您只需执行 $rank - 1
即可将其减去 1 存储在变量中而不更改主值。
foreach ($standings as $name => $score) {
if ($score != $prev_score) { //this score is not a tie
$count = 0;
$prev_score = $score;
$rankings[$name] = array('score' => $score, 'rank' => $rank);
} else { //this score is a tie
$prev_score = $score;
$rank--; // Decrements here
if ($count++ == 0) {
$tie_rank = $rank;
}
$rankings[$name] = array('score' => $score, 'rank' => $tie_rank);
}
$rank++;
}
return $rankings;