Ranking methods: Difference between revisions

jq
(Added zkl)
(jq)
Line 578:
5,0 41 Barry
7,0 39 Stephen</pre>
=={{header|jq}}==
We assume the list of players and their scores has already been sorted,
and that the list takes the form:
[ player1, score1, player2, score2, ...]
 
For the sake of brevity, only the ranks are printed.
<lang jq>
# Ties share what would have been their first ordinal number
def standard_ranking:
. as $raw
| ([range(1;length;2) | $raw[.]]) as $scores
| reduce range(1; $scores|length) as $i
([1]; if $scores[$i - 1] == $scores[$i] then . + [.[-1]]
else . + [$i + 1]
end) ;
 
def modified_ranking:
# The helper function resolves [ranks, tentative]
# by appending the ranks of the ties to "ranks"
def resolve:
(.[1] | length) as $length
| if $length == 0 then .[0]
else .[1][-1] as $max
| .[0] + ( .[1] | map( $max) )
end ;
. as $raw
| ([range(1;length;2) | $raw[.]]) as $scores
| reduce range(1; $scores|length) as $i
# state: [ranks, tentative]
([ [], [1] ];
if $scores[$i - 1] == $scores[$i] then [.[0], .[1] + [ $i + 1 ]]
else [ resolve, [ $i + 1 ] ]
end )
| resolve ;
 
def dense_ranking: # next available
. as $raw
| ([range(1;length;2) | $raw[.]]) as $scores
| reduce range(1; $scores|length) as $i
([1]; if $scores[$i - 1] == $scores[$i] then . + [.[-1]]
else . + [ .[-1] + 1]
end );
 
def ordinal_ranking: # unfair to some!
[ range(1; 1 + length/2) ] ;
 
def fractional_ranking:
# The helper function resolves [ranks, tentative]
# by appending the averages of the tentative ranks to "ranks"
def resolve:
(.[1] | length) as $length
| if $length == 0 then .[0]
else (.[1] | add / $length) as $avg
| .[0] + ( .[1] | map( $avg) )
end ;
. as $raw
| ([range(1;length;2) | $raw[.]]) as $scores
| reduce range(1; $scores|length) as $i
# state: [ranks, tentative]
([ [], [1] ];
if $scores[$i - 1] == $scores[$i] then [.[0], .[1] + [ $i + 1 ]]
else [ resolve, [ $i + 1 ] ]
end )
| resolve ;</lang>Task<lang jq>def raw:
[
"Solomon", 44,
"Jason" , 42,
"Errol" , 42,
"Garry" , 41,
"Bernard", 41,
"Barry" , 41,
"Stephen", 39
] ;
 
def task:
"standard: \( raw | standard_ranking)",
"modified: \( raw | modified_ranking)",
"dense: \( raw | dense_ranking)",
"ordinal: \( raw | ordinal_ranking)",
"fractional: \( raw | fractional_ranking)" ;
task
</lang>
{{Out}}
standard: [1,2,2,4,4,4,7]
modified: [1,3,3,6,6,6,7]
dense: [1,2,2,3,3,3,4]
ordinal: [1,2,3,4,5,6,7]
fractional: [1,2.5,2.5,5,5,5,7]
 
=={{header|Perl 6}}==
2,492

edits