Jump to content

Ranking methods: Difference between revisions

no edit summary
(→‎{{header|Ruby}}: small improvement)
No edit summary
Line 1,302:
5.0 => ["Garry", "Bernard", "Barry"]
7.0 => ["Stephen"]</pre>
 
=={{header|PowerShell}}==
<lang PowerShell>
function Get-Ranking
{
[CmdletBinding(DefaultParameterSetName="Standard")]
[OutputType([PSCustomObject])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string]
$InputObject,
 
[Parameter(Mandatory=$false,
ParameterSetName="Standard")]
[switch]
$Standard,
 
[Parameter(Mandatory=$false,
ParameterSetName="Modified")]
[switch]
$Modified,
 
[Parameter(Mandatory=$false,
ParameterSetName="Dense")]
[switch]
$Dense,
 
[Parameter(Mandatory=$false,
ParameterSetName="Ordinal")]
[switch]
$Ordinal,
 
[Parameter(Mandatory=$false,
ParameterSetName="Fractional")]
[switch]
$Fractional
)
 
Begin
{
function Get-OrdinalRank ([PSCustomObject[]]$Values)
{
for ($i = 0; $i -lt $Values.Count; $i++)
{
$Values[$i].Rank = $i + 1
}
 
$Values
}
 
function Get-Rank ([PSCustomObject[]]$Scores)
{
foreach ($score in $Scores)
{
$score.Group | ForEach-Object {$_.Rank = $score.Rank}
}
 
$Scores.Group
}
 
function New-Competitor ([string]$Name, [int]$Score, [int]$Rank = 0)
{
[PSCustomObject]@{
Name = $Name
Score = $Score
Rank = $Rank
}
}
 
$competitors = @()
$scores = @()
}
Process
{
@($input) | ForEach-Object {$competitors += New-Competitor -Name $_.Split()[1] -Score $_.Split()[0]}
}
End
{
$scores = $competitors |
Sort-Object -Property Score -Descending |
Group-Object -Property Score |
Select-Object -Property @{Name="Score"; Expression={[int]$_.Name}}, @{Name="Rank"; Expression={0}}, Count, Group
 
switch ($PSCmdlet.ParameterSetName)
{
"Standard"
{
$rank = 1
 
for ($i = 0; $i -lt $scores.Count; $i++)
{
$scores[$i].Rank = $rank
$rank += $scores[$i].Count
}
 
Get-Rank $scores
}
"Modified"
{
$rank = 0
 
foreach ($score in $scores)
{
$rank = $score.Count + $rank
$score.Rank = $rank
}
 
Get-Rank $scores
}
"Dense"
{
for ($i = 0; $i -lt $scores.Count; $i++)
{
$scores[$i].Rank = $i + 1
}
 
Get-Rank $scores
}
"Ordinal"
{
Get-OrdinalRank $competitors
}
"Fractional"
{
Get-OrdinalRank $competitors | Group-Object -Property Score | ForEach-Object {
if ($_.Count -gt 1)
{
$rank = ($_.Group.Rank | Measure-Object -Average).Average
 
foreach ($competitor in $_.Group)
{
$competitor.Rank = $rank
}
}
}
 
$competitors
}
}
}
}
</lang>
<lang PowerShell>
$scores = "44 Solomon","42 Jason","42 Errol","41 Garry","41 Bernard","41 Barry","39 Stephen"
</lang>
<lang PowerShell>
$scores | Get-Ranking -Standard
</lang>
{{Out}}
<pre>
Name Score Rank
---- ----- ----
Solomon 44 1
Jason 42 2
Errol 42 2
Garry 41 4
Bernard 41 4
Barry 41 4
Stephen 39 7
</pre>
<lang PowerShell>
$scores | Get-Ranking -Modified
</lang>
{{Out}}
<pre>
Name Score Rank
---- ----- ----
Solomon 44 1
Jason 42 3
Errol 42 3
Garry 41 6
Bernard 41 6
Barry 41 6
Stephen 39 7
</pre>
<lang PowerShell>
$scores | Get-Ranking -Dense
</lang>
{{Out}}
<pre>
Name Score Rank
---- ----- ----
Solomon 44 1
Jason 42 2
Errol 42 2
Garry 41 3
Bernard 41 3
Barry 41 3
Stephen 39 4
</pre>
<lang PowerShell>
$scores | Get-Ranking -Ordinal
</lang>
{{Out}}
<pre>
Name Score Rank
---- ----- ----
Solomon 44 1
Jason 42 2
Errol 42 3
Garry 41 4
Bernard 41 5
Barry 41 6
Stephen 39 7
</pre>
<lang PowerShell>
$scores | Get-Ranking -Fractional
</lang>
{{Out}}
<pre>
Name Score Rank
---- ----- ----
Solomon 44 1
Jason 42 2.5
Errol 42 2.5
Garry 41 5
Bernard 41 5
Barry 41 5
Stephen 39 7
</pre>
 
 
=={{header|Python}}==
308

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.