2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -2,6 +2,7 @@ The numerical rank of competitors in a competition shows if one is better than,
|
|||
|
||||
The numerical rank of a competitor can be assigned in several [[wp:Ranking|different ways]].
|
||||
|
||||
|
||||
;Task:
|
||||
The following scores are accrued for all competitors of a competition (in best-first order):
|
||||
<pre>44 Solomon
|
||||
|
|
@ -18,6 +19,9 @@ For each of the following ranking methods, create a function/method/procedure/su
|
|||
# Dense. (Ties share the next available integer).
|
||||
# Ordinal. ((Competitors take the next available integer. Ties are not treated otherwise).
|
||||
# Fractional. (Ties share the mean of what would have been their ordinal numbers).
|
||||
|
||||
<br>
|
||||
See the [[wp:Ranking|wikipedia article]] for a fuller description.
|
||||
|
||||
Show here, on this page, the ranking of the test scores under each of the numbered ranking methods.
|
||||
<br><br>
|
||||
|
|
|
|||
33
Task/Ranking-methods/Elixir/ranking-methods.elixir
Normal file
33
Task/Ranking-methods/Elixir/ranking-methods.elixir
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
defmodule Ranking do
|
||||
def methods(data) do
|
||||
IO.puts "stand.\tmod.\tdense\tord.\tfract."
|
||||
Enum.group_by(data, fn {score,_name} -> score end)
|
||||
|> Enum.map(fn {score,pairs} ->
|
||||
names = Enum.map(pairs, fn {_,name} -> name end) |> Enum.reverse
|
||||
{score, names}
|
||||
end)
|
||||
|> Enum.sort_by(fn {score,_} -> -score end)
|
||||
|> Enum.with_index
|
||||
|> Enum.reduce({1,0,0}, fn {{score, names}, i}, {s_rnk, m_rnk, o_rnk} ->
|
||||
d_rnk = i + 1
|
||||
m_rnk = m_rnk + length(names)
|
||||
f_rnk = ((s_rnk + m_rnk) / 2) |> to_string |> String.replace(".0","")
|
||||
o_rnk = Enum.reduce(names, o_rnk, fn name,acc ->
|
||||
IO.puts "#{s_rnk}\t#{m_rnk}\t#{d_rnk}\t#{acc+1}\t#{f_rnk}\t#{score} #{name}"
|
||||
acc + 1
|
||||
end)
|
||||
{s_rnk+length(names), m_rnk, o_rnk}
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
~w"44 Solomon
|
||||
42 Jason
|
||||
42 Errol
|
||||
41 Garry
|
||||
41 Bernard
|
||||
41 Barry
|
||||
39 Stephen"
|
||||
|> Enum.chunk(2)
|
||||
|> Enum.map(fn [score,name] -> {String.to_integer(score),name} end)
|
||||
|> Ranking.methods
|
||||
59
Task/Ranking-methods/JavaScript/ranking-methods-2.js
Normal file
59
Task/Ranking-methods/JavaScript/ranking-methods-2.js
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
((() => {
|
||||
const xs = 'Solomon Jason Errol Garry Bernard Barry Stephen'.split(' '),
|
||||
ns = [44, 42, 42, 41, 41, 41, 39];
|
||||
|
||||
const sorted = xs.map((x, i) => ({
|
||||
name: x,
|
||||
score: ns[i]
|
||||
}))
|
||||
.sort((a, b) => {
|
||||
const c = b.score - a.score;
|
||||
return c ? c : a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
|
||||
});
|
||||
|
||||
const names = sorted.map(x => x.name),
|
||||
scores = sorted.map(x => x.score),
|
||||
reversed = scores.slice(0)
|
||||
.reverse(),
|
||||
unique = scores.filter((x, i) => scores.indexOf(x) === i);
|
||||
|
||||
// RANKINGS AS FUNCTIONS OF SCORES: SORTED, REVERSED AND UNIQUE
|
||||
|
||||
// rankings :: Int -> Int -> Dictonary
|
||||
const rankings = (score, index) => ({
|
||||
name: names[index],
|
||||
score,
|
||||
Ordinal: index + 1,
|
||||
Standard: scores.indexOf(score) + 1,
|
||||
Modified: reversed.length - reversed.indexOf(score),
|
||||
Dense: unique.indexOf(score) + 1,
|
||||
|
||||
Fractional: (n => (
|
||||
(scores.indexOf(n) + 1) +
|
||||
(reversed.length - reversed.indexOf(n))
|
||||
) / 2)(score)
|
||||
});
|
||||
|
||||
// tbl :: [[[a]]]
|
||||
const tbl = [
|
||||
'Name Score Standard Modified Dense Ordinal Fractional'.split(' ')
|
||||
].concat(scores.map(rankings)
|
||||
.reduce((a, x) => a.concat([
|
||||
[x.name, x.score,
|
||||
x.Standard, x.Modified, x.Dense, x.Ordinal, x.Fractional
|
||||
]
|
||||
]), []));
|
||||
|
||||
// wikiTable :: [[[a]]] -> Bool -> String -> String
|
||||
const wikiTable = (lstRows, blnHeaderRow, strStyle) =>
|
||||
`{| class="wikitable" ${strStyle ? 'style="' + strStyle + '"' : ''}
|
||||
${lstRows.map((lstRow, iRow) => {
|
||||
const strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
|
||||
|
||||
return '\n|-\n' + strDelim + ' ' + lstRow
|
||||
.map(v => typeof v === 'undefined' ? ' ' : v)
|
||||
.join(' ' + strDelim + strDelim + ' ');
|
||||
}).join('')}\n|}`;
|
||||
|
||||
return wikiTable(tbl, true, 'text-align:center');
|
||||
}))();
|
||||
19
Task/Ranking-methods/Mathematica/ranking-methods.math
Normal file
19
Task/Ranking-methods/Mathematica/ranking-methods.math
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
data = Transpose@{{44, 42, 42, 41, 41, 41, 39}, {"Solomon", "Jason",
|
||||
"Errol", "Garry", "Bernard", "Barry", "Stephen"}};
|
||||
|
||||
rank[data_, type_] :=
|
||||
Module[{t = Transpose@{Sort@data, Range[Length@data, 1, -1]}},
|
||||
Switch[type,
|
||||
"standard", data/.Rule@@@First/@SplitBy[t, First],
|
||||
"modified", data/.Rule@@@Last/@SplitBy[t, First],
|
||||
"dense", data/.Thread[#->Range[Length@#]]&@SplitBy[t, First][[All, 1, 1]],
|
||||
"ordinal", Reverse@Ordering[data],
|
||||
"fractional", data/.Rule@@@(Mean[#]/.{a_Rational:>N[a]}&)/@ SplitBy[t, First]]]
|
||||
|
||||
fmtRankedData[data_, type_] :=
|
||||
Labeled[Grid[
|
||||
SortBy[ArrayFlatten@{{Transpose@{rank[data[[All, 1]], type]},
|
||||
data}}, First], Alignment->Left], type<>" ranking:", Top]
|
||||
|
||||
Grid@{fmtRankedData[data, #] & /@ {"standard", "modified", "dense",
|
||||
"ordinal", "fractional"}}
|
||||
12
Task/Ranking-methods/PARI-GP/ranking-methods.pari
Normal file
12
Task/Ranking-methods/PARI-GP/ranking-methods.pari
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
standard(v)=v=vecsort(v,1,4); my(last=v[1][1]+1); for(i=1,#v, v[i][1]=if(v[i][1]<last,last=v[i][1]; i, v[i-1][1])); v;
|
||||
modified(v)=v=vecsort(v,1,4); my(last=v[#v][1]-1); forstep(i=#v,1,-1, v[i][1]=if(v[i][1]>last,last=v[i][1]; i, v[i+1][1])); v;
|
||||
dense(v)=v=vecsort(v,1,4); my(last=v[1][1]+1,rank); for(i=1,#v, v[i][1]=if(v[i][1]<last,last=v[i][1]; rank++, rank)); v;
|
||||
ordinal(v)=v=vecsort(v,1,4); for(i=1,#v,v[i][1]=i); v;
|
||||
fractional(v)=my(a=standard(v),b=modified(v)); vector(#v,i,[(a[i][1]+b[i][1])/2,v[i][2]]);
|
||||
|
||||
v=[[44,"Solomon"], [42,"Jason"], [42,"Errol"], [41,"Garry"], [41,"Bernard"], [41,"Barry"], [39,"Stephen"]];
|
||||
standard(v)
|
||||
modified(v)
|
||||
dense(v)
|
||||
ordinal(v)
|
||||
fractional(v)
|
||||
142
Task/Ranking-methods/PowerShell/ranking-methods-1.psh
Normal file
142
Task/Ranking-methods/PowerShell/ranking-methods-1.psh
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1
Task/Ranking-methods/PowerShell/ranking-methods-2.psh
Normal file
1
Task/Ranking-methods/PowerShell/ranking-methods-2.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
$scores = "44 Solomon","42 Jason","42 Errol","41 Garry","41 Bernard","41 Barry","39 Stephen"
|
||||
1
Task/Ranking-methods/PowerShell/ranking-methods-3.psh
Normal file
1
Task/Ranking-methods/PowerShell/ranking-methods-3.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
$scores | Get-Ranking -Standard
|
||||
1
Task/Ranking-methods/PowerShell/ranking-methods-4.psh
Normal file
1
Task/Ranking-methods/PowerShell/ranking-methods-4.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
$scores | Get-Ranking -Modified
|
||||
1
Task/Ranking-methods/PowerShell/ranking-methods-5.psh
Normal file
1
Task/Ranking-methods/PowerShell/ranking-methods-5.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
$scores | Get-Ranking -Dense
|
||||
1
Task/Ranking-methods/PowerShell/ranking-methods-6.psh
Normal file
1
Task/Ranking-methods/PowerShell/ranking-methods-6.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
$scores | Get-Ranking -Ordinal
|
||||
1
Task/Ranking-methods/PowerShell/ranking-methods-7.psh
Normal file
1
Task/Ranking-methods/PowerShell/ranking-methods-7.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
$scores | Get-Ranking -Fractional
|
||||
|
|
@ -5,19 +5,17 @@ ar = "44 Solomon
|
|||
41 Bernard
|
||||
41 Barry
|
||||
39 Stephen".lines.map{|line| line.split}
|
||||
|
||||
grouped = ar.group_by{|pair| pair.delete_at(0).to_i}
|
||||
grouped = ar.group_by{|pair| pair.shift.to_i}
|
||||
s_rnk = 1
|
||||
m_rnk = o_rnk = f_rnk = 0
|
||||
m_rnk = o_rnk = 0
|
||||
puts "stand.\tmod.\tdense\tord.\tfract."
|
||||
|
||||
grouped.each.with_index do |(score, names), i|
|
||||
d_rnk = i + 1
|
||||
grouped.each.with_index(1) do |(score, names), d_rnk|
|
||||
m_rnk += names.flatten!.size
|
||||
f_rnk = (s_rnk + m_rnk)/2.0
|
||||
names.each do |name|
|
||||
o_rnk += 1
|
||||
puts "#{s_rnk}\t#{m_rnk}\t#{d_rnk}\t#{o_rnk}\t#{f_rnk.to_s.gsub(".0","")}\t#{score} #{name}"
|
||||
puts "#{s_rnk}\t#{m_rnk}\t#{d_rnk}\t#{o_rnk}\t#{f_rnk.to_s.sub(".0","")}\t#{score} #{name}"
|
||||
end
|
||||
s_rnk += names.size
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue