Ranking methods: Difference between revisions

m
(Ranking methods en FreeBASIC)
m (→‎{{header|Wren}}: Minor tidy)
 
(13 intermediate revisions by 9 users not shown)
Line 31:
<br><br>
 
=={{header|AppleScript11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F mc_rank([(Int, String)] iterable)
<lang applescript>(*
‘Modified competition ranking’
The five ranking methods are implemented here as script objects sharing inherited code rather than as simple
[(Float, (Int, String))] r
handlers, although there appears to be little advantage either way. This way needs fewer lines of code
V lastresult = -1
but more lines of explanatory comments!
[(Int, String)] fifo
*)
L(item) iterable
use AppleScript version "2.4" -- Mac OS 10.11 (Yosemite) or later (for these 'use' commands).
I item[0] == lastresult
use sorter : script "Custom Iterative Ternary Merge Sort" -- <https://macscripter.net/viewtopic.php?pid=194430#p194430>
fifo [+]= item
E
V n = L.index
L !fifo.empty
r.append((n, fifo.pop(0)))
lastresult = item[0]
fifo [+]= item
L !fifo.empty
r.append((iterable.len, fifo.pop(0)))
R r
 
F sc_rank([(Int, String)] iterable)
‘Standard competition ranking’
[(Float, (Int, String))] r
V lastresult = -1
V lastrank = -1
L(item) iterable
I item[0] == lastresult
r.append((lastrank, item))
E
V n = L.index + 1
r.append((n, item))
lastresult = item[0]
lastrank = n
R r
 
F d_rank([(Int, String)] iterable)
‘Dense ranking’
[(Float, (Int, String))] r
V lastresult = -1
V lastrank = 0
L(item) iterable
I item[0] == lastresult
r.append((lastrank, item))
E
lastresult = item[0]
lastrank++
r.append((lastrank, item))
R r
 
F o_rank([(Int, String)] iterable)
‘Ordinal ranking’
R enumerate(iterable, 1).map((i, item) -> ((Float(i), item)))
 
F f_rank([(Int, String)] iterable)
‘Fractional ranking’
[(Float, (Int, String))] r
V last = -1
[(Int, (Int, String))] fifo
L(item) iterable
I item[0] != last
I !fifo.empty
V mean = Float(sum(fifo.map(f -> f[0]))) / fifo.len
L !fifo.empty
r.append((mean, fifo.pop(0)[1]))
last = item[0]
fifo.append((L.index + 1, item))
I !fifo.empty
V mean = sum(fifo.map(f -> f[0])) / fifo.len
L !fifo.empty
r.append((mean, fifo.pop(0)[1]))
R r
 
V scores = [(44, ‘Solomon’),
(42, ‘Jason’),
(42, ‘Errol’),
(41, ‘Garry’),
(41, ‘Bernard’),
(41, ‘Barry’),
(39, ‘Stephen’)]
 
print("\nScores to be ranked (best first):")
L(n, s) scores
print(‘ #2 #.’.format(n, s))
 
L(ranker, ranking_method) [(sc_rank, ‘Standard competition ranking’),
(mc_rank, ‘Modified competition ranking’),
(d_rank, ‘Dense ranking’),
(o_rank, ‘Ordinal ranking’),
(f_rank, ‘Fractional ranking’)]
print("\n#.:".format(ranking_method))
L(rank, score) ranker(scores)
print(‘ #3, (#., #.)’.format(rank, score[0], score[1]))</syntaxhighlight>
 
{{out}}
<pre>
 
Scores to be ranked (best first):
44 Solomon
42 Jason
42 Errol
41 Garry
41 Bernard
41 Barry
39 Stephen
 
Standard competition ranking:
1, (44, Solomon)
2, (42, Jason)
2, (42, Errol)
4, (41, Garry)
4, (41, Bernard)
4, (41, Barry)
7, (39, Stephen)
 
Modified competition ranking:
1, (44, Solomon)
3, (42, Jason)
3, (42, Errol)
6, (41, Garry)
6, (41, Bernard)
6, (41, Barry)
7, (39, Stephen)
 
Dense ranking:
1, (44, Solomon)
2, (42, Jason)
2, (42, Errol)
3, (41, Garry)
3, (41, Bernard)
3, (41, Barry)
4, (39, Stephen)
 
Ordinal ranking:
1, (44, Solomon)
2, (42, Jason)
3, (42, Errol)
4, (41, Garry)
5, (41, Bernard)
6, (41, Barry)
7, (39, Stephen)
 
Fractional ranking:
1, (44, Solomon)
2.5, (42, Jason)
2.5, (42, Errol)
5, (41, Garry)
5, (41, Bernard)
5, (41, Barry)
7, (39, Stephen)
</pre>
 
=={{header|ALGOL 68}}==
As with some but not all of the other samples, the ranking procedures here assume the data is already sorted. The procedures do check for empty data sets, though.
<syntaxhighlight lang="algol68">
BEGIN # rank some scores by various methods #
# MODE to hold the scores #
MODE RESULT = STRUCT( INT score, STRING name );
# returns the standard ranking of s #
PROC standard ranking = ( []RESULT s )[]INT:
IF LWB s > UPB s THEN []INT() # no scores #
ELSE # have some scores #
[ LWB s : UPB s ]INT ranked;
INT position := 1;
ranked[ LWB s ] := position;
FOR i FROM LWB s + 1 TO UPB s DO
ranked[ i ] := IF score OF s[ i ] = score OF s[ i - 1 ] THEN
# same score as the previous #
position
ELSE
# different score, increase the position #
position := i
FI
OD;
ranked
FI # standard ranking # ;
# returns the modified ranking of s #
PROC modified ranking = ( []RESULT s )[]INT:
IF LWB s > UPB s THEN []INT() # no scores #
ELSE # have some scores #
[ LWB s : UPB s ]INT ranked;
INT position := ( UPB s + 1 ) - LWB s;
ranked[ UPB s ] := position;
FOR i FROM UPB s - 1 BY -1 TO LWB s DO
ranked[ i ] := IF score OF s[ i ] = score OF s[ i + 1 ] THEN
# same score as the previous #
position
ELSE
# different score, decrease the position #
position := i
FI
OD;
ranked
FI # modified ranking # ;
# returns the debse ranking of s #
PROC dense ranking = ( []RESULT s )[]INT:
IF LWB s > UPB s THEN []INT() # no scores #
ELSE # have some scores #
[ LWB s : UPB s ]INT ranked;
INT position := 1;
ranked[ LWB s ] := position;
FOR i FROM LWB s + 1 TO UPB s DO
ranked[ i ] := IF score OF s[ i ] = score OF s[ i - 1 ] THEN
# same score as the previous #
position
ELSE
# different score, increase the position #
position +:= 1
FI
OD;
ranked
FI # dense ranking # ;
# returns the ordinal ranking of s #
PROC ordinal ranking = ( []RESULT s )[]INT:
IF LWB s > UPB s THEN []INT() # no scores #
ELSE # have some scores #
[ LWB s : UPB s ]INT ranked;
INT position := 0;
FOR i FROM LWB s TO UPB s DO
ranked[ i ] := position +:= 1
OD;
ranked
FI # ordinal ranking # ;
# regturns the fractional ranking of s #
PROC fractional ranking = ( []RESULT s )[]REAL:
IF LWB s > UPB s THEN []REAL() # no scores #
ELSE # have some scores #
[ LWB s : UPB s ]REAL ranked;
REAL position := 1;
FOR i FROM LWB s TO UPB s DO
ranked[ i ]
:= IF IF i = LWB s
THEN FALSE
ELSE score OF s[ i ] = score OF s[ i - 1 ]
FI
THEN
# same score as the previous #
ranked[ i - 1 ]
ELSE
# first score or different score to the previous #
INT same count := 1;
INT sum := i;
FOR j FROM i + 1 TO UPB s
WHILE score OF s[ i ] = score OF s[ j ]
DO
same count +:= 1;
sum +:= j
OD;
sum / same count
FI
OD;
ranked
FI # fractional ranking # ;
# shows the integer ranking of some scores #
PROC show integral ranking = ( []RESULT s, []INT ranking, STRING title )VOID:
BEGIN
print( ( title, " competition ranking:", newline ) );
FOR i FROM LWB s TO UPB s DO
print( ( whole( ranking[ i ], -3 )
, ": "
, whole( score OF s[ i ], -3 )
, " "
, name OF s[ i ]
, newline
)
)
OD;
print( ( newline ) )
END # show integral ranking # ;
# shows the real ranking of some scores #
PROC show real ranking = ( []RESULT s, []REAL ranking, STRING title )VOID:
BEGIN
print( ( title, " competition ranking:", newline ) );
FOR i FROM LWB s TO UPB s DO
print( ( IF INT integer rank = ENTIER ranking[ i ];
integer rank = ranking[ i ]
THEN
whole( integer rank, -3 ) + " "
ELSE
fixed( ranking[ i ], -6, 2 )
FI
, ": "
, whole( score OF s[ i ], -3 ), " "
, name OF s[ i ]
, newline
)
)
OD;
print( ( newline ) )
END # show real ranking # ;
 
# scores to rank - task test cases #
[]RESULT scores = ( ( 44, "Solomon" )
, ( 42, "Jason" )
, ( 42, "Errol" )
, ( 41, "Garry" )
, ( 41, "Bernard" )
, ( 41, "Barry" )
, ( 39, "Stephen" )
);
show integral ranking( scores, standard ranking( scores ), "standard" );
show integral ranking( scores, modified ranking( scores ), "modified" );
show integral ranking( scores, dense ranking( scores ), "dense" );
show integral ranking( scores, ordinal ranking( scores ), "ordinal" );
show real ranking( scores, fractional ranking( scores ), "fractional" )
 
END
</syntaxhighlight>
{{out}}
<pre>
standard competition ranking:
1: 44 Solomon
2: 42 Jason
2: 42 Errol
4: 41 Garry
4: 41 Bernard
4: 41 Barry
7: 39 Stephen
 
modified competition ranking:
1: 44 Solomon
3: 42 Jason
3: 42 Errol
6: 41 Garry
6: 41 Bernard
6: 41 Barry
7: 39 Stephen
 
dense competition ranking:
1: 44 Solomon
2: 42 Jason
2: 42 Errol
3: 41 Garry
3: 41 Bernard
3: 41 Barry
4: 39 Stephen
 
ordinal competition ranking:
1: 44 Solomon
2: 42 Jason
3: 42 Errol
4: 41 Garry
5: 41 Bernard
6: 41 Barry
7: 39 Stephen
 
fractional competition ranking:
1 : 44 Solomon
2.50: 42 Jason
2.50: 42 Errol
5 : 41 Garry
5 : 41 Bernard
5 : 41 Barry
7 : 39 Stephen
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- Mac OS 10.9 (Mavericks) or later.
use sorter : script ¬
"Custom Iterative Ternary Merge Sort" -- <www.macscripter.net/t/timsort-and-nigsort/71383/3>
 
(* The ranking methods are implemented as script objects sharing inherited code. *)
script standardRanking
-- Properties and handlers inherited or overridden by the other script objects.
-- The 'referencesreference' areis to valuesa value that won't be availableexist until 'results' is set to a list.
-- 'me' and 'my' referpertain to the script object using the code at the time.
property results : missing value
property startIndex : 1
property endIndex : a reference to lengthmy ofresults's (a reference to results)length
property step : 1
property currentRank : missing value
Line 55 ⟶ 407:
on resultsFrom(theScores)
copy theScores to my results
-- The task description specifies that the input list is already ordered, but that's not assumed here.
-- Additionally, competitors with equal scores are arranged alphabetically by name.
-- The comparer here is 'me' because the current script object contains the isGreater(a, b) handler to be used.
tell sorter to sort(my results, my startIndex, my endIndex, {comparer:me})
set my currentScore to scoremy ofresults's item (my startIndex)'s of my resultsscore
set my currentRank to my startIndex's contents
repeat with i from (my startIndex) to (my endIndex) by (my step)
my rankResult(i)
end repeat
set r to my results
-- Since these script objects are assigned to top-level variables in a running script, make sure the potentially
-- long 'results' list isn't saved back to the script file with them at the end of the run. This precaution isn't
-- necessary in a library script or if the scripts are instantiated and assigned to local variables at run time.
set localVariable to my results
set my results to missing value
return localVariabler
end resultsFrom
-- Comparison handler used by the sort. The returned boolean indicates whether or not record 'a' should go after record 'b'.
on isGreater(a, b)
if (a's score is less than< b's score) then return true
return ((a's score is equal to= b's score) and (a's competitor comes after b's competitor))
end isGreater
-- Specialist rankingRanking handler. Inherited by the modifiedRanking script, but; overridden by the others.
on rankResult(i)
set thisResult to my results's item i of my results
set thisScore to thisResult's score
if (thisScore is not currentScore) then
Line 86 ⟶ 432:
set my currentScore to thisScore
end if
set my results's item i of my results to thisResult & {rank:my currentRank}
end rankResult
end script
 
script modifiedRanking -- Uses the standardRanking code, but works backwards through the results list.
property parent : standardRanking
property startIndex : a reference to lengthmy ofresults's (a reference to results)length
property endIndex : 1
property step : -1
Line 101 ⟶ 447:
on rankResult(i)
set thisResult to my results's item i of my results
set thisScore to thisResult's score
if (thisScore is not my currentScore) then
Line 107 ⟶ 453:
set my currentScore to thisScore
end if
set my results's item i of my results to thisResult & {rank:my currentRank}
end rankResult
end script
Line 115 ⟶ 461:
on rankResult(i)
set my results's item i ofto (my results's to (item i of my results) & {rank:i}
end rankResult
end script
Line 123 ⟶ 469:
on rankResult(i)
set thisResult to my results's item i of my results
set thisScore to thisResult's score
if (thisScore is not my currentScore) then
-- i and currentRank are simultaneously indices and ranks.
-- The average of any run of consecutive integers is that of the first and last.
set average to (i - 1 + (my currentRank)) / 2
repeat with j from (my currentRank) to (i - 1)
set rankmy ofresults's item j's of my resultsrank to average
end repeat
set my currentRank to i
set my currentScore to thisScore
end if
set my results's item i of my results to thisResult & {rank:i as real}
end rankResult
end script
Line 143 ⟶ 488:
set rankings to {type}
repeat with thisResult in theResults
set end of rankings to (thisResult's rank as text) & tab & thisResult's competitor & " (" & thisResult's score & ")"¬
thisResult's competitor & " (" & thisResult's score & ")"
end repeat
return joinStringsjoin(rankings, linefeed)
end formatRankings
 
on join(lst, delim)
on joinStrings(listOfText, delimiter)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiterdelim
set outputtxt to listOfTextlst as text
set AppleScript's text item delimiters to astid
return txt
end join
return output
end joinStrings
 
local theScores, output
set theScores to {{score:44, competitor:"Solomon"}, {score:42, competitor:"Jason"}, {score:42, competitor:"Errol"}, ¬
{score:4142, competitor:"GarryErrol"}, {score:41, competitor:"BernardGarry"}, {score:41, competitor:"BarryBernard"}, ¬
{score:41, competitor:"Barry"}, {score:39, competitor:"Stephen"}}
set output to {}¬
set end of output to formatRankings("Standard ranking:", standardRanking's resultsFrom(theScores)), ¬
set end of output to formatRankings("Modified ranking:", modifiedRanking's resultsFrom(theScores)), ¬
set end of output to formatRankings("Dense ranking:", denseRanking's resultsFrom(theScores)), ¬
set end of output to formatRankings("Ordinal ranking:", ordinalRanking's resultsFrom(theScores)), ¬
set end of output to formatRankings("Fractional ranking:", fractionalRanking's resultsFrom(theScores)) ¬
}
return joinStrings(output, linefeed & linefeed)</lang>
return join(output, linefeed & linefeed)</syntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"Standard ranking:
1 Solomon (44)
2 Errol (42)
Line 214 ⟶ 560:
5.0 Bernard (41)
5.0 Garry (41)
7.0 Stephen (39)"</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Rank(data, opt:=1){ ; opt = 1 Standard (default), 2 Modified, 3 Dense, 4 Ordinal, 5 Fractional
for index, val in StrSplit(data, "`n", "`r") {
RegExMatch(val, "^(\d+)\s+(.*)", Match)
Line 238 ⟶ 584:
}
return Res%opt%
}</langsyntaxhighlight>
Example:<langsyntaxhighlight AutoHotkeylang="autohotkey">data =
(
44 Solomon
Line 256 ⟶ 602:
. "`nOrdinal Ranking:`n" Rank(data, 4)
. "`nFractional Ranking:`n" Rank(data, 5)
return</langsyntaxhighlight>
Output:<pre>Standard Ranking:
1 44 Solomon
Line 309 ⟶ 655:
matrix, with the ranking added.
 
<langsyntaxhighlight lang="apl">standard ← ∊∘(⌊\¨⊢⊂⍳∘≢)∘(1,2≠/⊢)∘(1⌷[2]⊢),⊢
modified ← ∊∘(⌈\∘⌽¨⊢⊂⍳∘≢)∘(1,2≠/⊢)∘(1⌷[2]⊢),⊢
dense ← (+\1,2≠/1⌷[2]⊢),⊢
ordinal ← ⍳∘≢,⊢
fractional ← ∊∘((≢(/∘⊢)+/÷≢)¨⊢⊂⍳∘≢)∘(1,2≠/⊢)∘(1⌷[2]⊢),⊢</langsyntaxhighlight>
 
{{out}}
Line 375 ⟶ 721:
{{trans|Python}}
This uses separate files for each method of ranking:
<langsyntaxhighlight lang="awk">##
## Dense ranking in file: ranking_d.awk
##
Line 475 ⟶ 821:
}
//{sc_rank()}
</syntaxhighlight>
</lang>
 
The input as a file <code>ranking.txt</code>:
Line 535 ⟶ 881:
 
=={{header|BASIC}}==
<langsyntaxhighlight BASIClang="basic">10 READ N
20 DIM S(N),N$(N),R(N)
30 FOR I=1 TO N: READ S(I),N$(I): NEXT
Line 584 ⟶ 930:
550 DATA 41,Bernard
560 DATA 41,Barry
570 DATA 39,Stephen</langsyntaxhighlight>
 
{{out}}
Line 635 ⟶ 981:
=={{header|C}}==
Takes the scores as input via a file, prints out usage on incorrect invocation.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 753 ⟶ 1,099:
return 0;
}
</syntaxhighlight>
</lang>
Input file, first row is number of records :
<pre>
Line 823 ⟶ 1,169:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 959 ⟶ 1,305:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Standard Rank
Line 1,008 ⟶ 1,354:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iomanip>
#include <iostream>
Line 1,183 ⟶ 1,529:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Standard Rank
Line 1,231 ⟶ 1,577:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
# List of competitors
Line 1,360 ⟶ 1,706:
print_nl();
n := n + 1;
end loop;</langsyntaxhighlight>
 
{{out}}
Line 1,410 ⟶ 1,756:
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import std.algorithm;
import std.stdio;
 
Line 1,576 ⟶ 1,922:
 
writeln;
}</langsyntaxhighlight>
 
{{out}}
Line 1,626 ⟶ 1,972:
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule Ranking do
def methods(data) do
IO.puts "stand.\tmod.\tdense\tord.\tfract."
Line 1,658 ⟶ 2,004:
|> Enum.chunk(2)
|> Enum.map(fn [score,name] -> {String.to_integer(score),name} end)
|> Ranking.methods</langsyntaxhighlight>
 
{{out}}
Line 1,674 ⟶ 2,020:
=={{header|Factor}}==
{{works with|Factor|0.99 2019-07-10}}
<langsyntaxhighlight lang="factor">USING: arrays assocs formatting fry generalizations io kernel
math math.ranges math.statistics math.vectors sequences
splitting.monotonic ;
Line 1,709 ⟶ 2,055:
[ [ print ] [ .rank nl ] bi* ] 2 5 mnapply ;
 
MAIN: ranking-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 1,760 ⟶ 2,106:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Data 44,"Solomon", 42,"Jason", 42,"Errol", 41,"Garry"
Data 41,"Bernard", 41,"Barry", 39,"Stephen"
Line 1,824 ⟶ 2,170:
MostarTabla
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,884 ⟶ 2,230:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,996 ⟶ 2,342:
show("\nOrdinal", OrdinalRank)
show("\nFractional", FractionalRank)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,046 ⟶ 2,392:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.List (groupBy, sortBy, intercalate)
 
type Item = (Int, String)
Line 2,134 ⟶ 2,480:
nicePrint "Dense:" $ dense test
nicePrint "Ordinal:" $ ordinal test
nicePrint "Fractional:" $ fractional test</langsyntaxhighlight>
{{Out}}
<pre>Standard:
Line 2,184 ⟶ 2,530:
Implementation:
 
<langsyntaxhighlight Jlang="j">competitors=:<;._1;._2]0 :0
44 Solomon
42 Jason
Line 2,202 ⟶ 2,548:
fractional=: #/.~ # ] (+/%#)/. #\
 
rank=:1 :'<"0@u@:scores,.]'</langsyntaxhighlight>
 
Note that we assume that the competitors are already in the right order. Also, of course (as is common when using J) we use the J command line, because that is portable across operating systems (for example: the OS command line is difficult to use on phones).
Line 2,208 ⟶ 2,554:
Task examples:
 
<langsyntaxhighlight Jlang="j"> standard rank competitors
┌─┬──┬───────┐
│1│44│Solomon│
Line 2,287 ⟶ 2,633:
├───┼──┼───────┤
│7 │39│Stephen│
└───┴──┴───────┘</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.util.*;
 
public class RankingMethods {
Line 2,365 ⟶ 2,711:
}
}
}</langsyntaxhighlight>
 
<pre>Standard ranking
Line 2,420 ⟶ 2,766:
( This version chooses to use a secondary (alphabetic) sort after the numeric sort by score. That does, of course, affect the ordinal placements for some players)
 
<langsyntaxhighlight JavaScriptlang="javascript">(function () {
var xs = 'Solomon Jason Errol Garry Bernard Barry Stephen'.split(' '),
Line 2,495 ⟶ 2,841:
return wikiTable(tbl, true, 'text-align:center');
})();</langsyntaxhighlight>
 
{{out}}
Line 2,520 ⟶ 2,866:
===ES6===
 
<langsyntaxhighlight JavaScriptlang="javascript">((() => {
const xs = 'Solomon Jason Errol Garry Bernard Barry Stephen'.split(' '),
ns = [44, 42, 42, 41, 41, 41, 39];
Line 2,578 ⟶ 2,924:
 
return wikiTable(tbl, true, 'text-align:center');
}))();</langsyntaxhighlight>
 
{| class="wikitable" style="text-align:center"
Line 2,607 ⟶ 2,953:
 
For the sake of brevity, only the ranks are printed.
<syntaxhighlight lang="jq">
<lang jq>
# Ties share what would have been their first ordinal number
def standard_ranking:
Line 2,664 ⟶ 3,010:
else [ resolve, [ $i + 1 ] ]
end )
| resolve ;</langsyntaxhighlight>Task<syntaxhighlight lang ="jq">def raw:
[
"Solomon", 44,
Line 2,683 ⟶ 3,029:
task
</syntaxhighlight>
</lang>
{{Out}}
standard: [1,2,2,4,4,4,7]
Line 2,693 ⟶ 3,039:
=={{header|Julia}}==
'''ties''', a helper function used by some of the ranking methods. It lists any duplicated scores.
<syntaxhighlight lang="julia">
<lang Julia>
function ties{T<:Real}(a::Array{T,1})
unique(a[2:end][a[2:end] .== a[1:end-1]])
end
</syntaxhighlight>
</lang>
<code>ties</code> assumes that the there are at least 2 scores in the list to be checked, and the calling functions are designed to avoid calls to it in this case.
 
'''Standard Ranking Function'''
<syntaxhighlight lang="julia">
<lang Julia>
function rankstandard{T<:Real}(a::Array{T,1})
r = collect(1:length(a))
Line 2,710 ⟶ 3,056:
return r
end
</syntaxhighlight>
</lang>
 
'''Modified Ranking Function'''
<syntaxhighlight lang="julia">
<lang Julia>
function rankmodified{T<:Real}(a::Array{T,1})
indexin(a, a)
end
</syntaxhighlight>
</lang>
 
'''Dense Ranking Function'''
<syntaxhighlight lang="julia">
<lang Julia>
function rankdense{T<:Real}(a::Array{T,1})
indexin(a, unique(a))
end
</syntaxhighlight>
</lang>
 
'''Ordinal Ranking Function'''
<syntaxhighlight lang="julia">
<lang Julia>
function rankordinal{T<:Real}(a::Array{T,1})
collect(1:length(a))
end
</syntaxhighlight>
</lang>
For ordinal ranking, there are a variety of ways of handling tied scores. I've taken the easy way out and assumed that the position in the list already reflects any tie-breaking policy. In this case, there is not much that needs to be done.
 
'''Fractional Ranking Function'''
<syntaxhighlight lang="julia">
<lang Julia>
function rankfractional{T<:Real}(a::Array{T,1})
r = float64(collect(1:length(a)))
Line 2,744 ⟶ 3,090:
return r
end
</syntaxhighlight>
</lang>
 
'''Main'''
<syntaxhighlight lang="julia">
<lang Julia>
scores = [44, 42, 42, 41, 41, 41, 39]
names = ["Solomon", "Jason", "Errol", "Garry",
Line 2,769 ⟶ 3,115:
println()
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,784 ⟶ 3,130:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
/* all ranking functions assume the array of Pairs is non-empty and already sorted by decreasing order of scores
Line 2,863 ⟶ 3,209:
printRankings("Ordinal ranking", ordinalRanking(scores), scores)
printFractionalRankings("Fractional ranking", fractionalRanking(scores), scores)
}</langsyntaxhighlight>
 
{{out}}
Line 2,913 ⟶ 3,259:
</pre>
 
=={{header|MathematicaKsh}}==
<syntaxhighlight lang="ksh">
<lang Mathematica>
#!/bin/ksh
data = Transpose@{{44, 42, 42, 41, 41, 41, 39}, {"Solomon", "Jason",
exec 2> /tmp/Ranking_methods.err
# Ranking methods
#
# # Standard. (Ties share what would have been their first ordinal number).
# # Modified. (Ties share what would have been their last ordinal number).
# # 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)
 
# # Variables:
#
typeset -a arr=( '44 Solomon' '42 Jason' '42 Errol' '41 Garry' '41 Bernard' '41 Barry' '39 Stephen' )
integer i
 
# # Functions:
#
 
# # Function _rankStandard(arr, rankarr) - retun arr with standard ranking
#
function _rankStandard {
typeset _ranked ; nameref _ranked="$1"
 
typeset _i _j _scr _currank _prevscr _shelf
integer _i _j _scr _currank=1 _prevscr
typeset -a _shelf
 
for ((_i=0; _i<${#arr[*]}; _i++)); do
_scr=${arr[_i]%\ *}
if (( _i>0 )) && (( _scr != _prevscr )); then
for ((_j=0; _j<${#_shelf[*]}; _j++)); do
_ranked+=( "${_currank} ${_shelf[_j]}" )
done
(( _currank+=${#_shelf[*]} ))
unset _shelf ; typeset -a _shelf
fi
_shelf+=( "${arr[_i]}" )
_prevscr=${_scr}
done
for ((_j=0; _j<${#_shelf[*]}; _j++)); do
_ranked+=( "${_currank} ${_shelf[_j]}" )
done
}
 
# # Function _rankModified(arr, rankarr) - retun arr with modified ranking
#
function _rankModified {
typeset _ranked ; nameref _ranked="$1"
 
typeset _i _j _scr _currank _prevscr _shelf
integer _i _j _scr _currank=0 _prevscr
typeset -a _shelf
 
for ((_i=0; _i<${#arr[*]}; _i++)); do
_scr=${arr[_i]%\ *}
if (( _i>0 )) && (( _scr != _prevscr )); then
for ((_j=0; _j<${#_shelf[*]}; _j++)); do
_ranked+=( "${_currank} ${_shelf[_j]}" )
done
unset _shelf ; typeset -a _shelf
fi
_shelf+=( "${arr[_i]}" )
(( _currank++ ))
_prevscr=${_scr}
done
for ((_j=0; _j<${#_shelf[*]}; _j++)); do
_ranked+=( "${_currank} ${_shelf[_j]}" )
done
}
 
# # Function _rankDense(arr, rankarr) - retun arr with dense ranking
#
function _rankDense {
typeset _ranked ; nameref _ranked="$1"
 
typeset _i _j _scr _currank _prevscr _shelf
integer _i _j _scr _currank=0 _prevscr
typeset -a _shelf
 
for ((_i=0; _i<${#arr[*]}; _i++)); do
_scr=${arr[_i]%\ *}
if (( _i>0 )) && (( _scr != _prevscr )); then
(( _currank++ ))
for ((_j=0; _j<${#_shelf[*]}; _j++)); do
_ranked+=( "${_currank} ${_shelf[_j]}" )
done
unset _shelf ; typeset -a _shelf
fi
_shelf+=( "${arr[_i]}" )
_prevscr=${_scr}
done
(( _currank++ ))
for ((_j=0; _j<${#_shelf[*]}; _j++)); do
_ranked+=( "${_currank} ${_shelf[_j]}" )
done
}
 
# # Function _rankOrdinal(arr, rankarr) - retun arr with ordinal ranking
#
function _rankOrdinal {
typeset _ranked ; nameref _ranked="$1"
typeset _i ; integer _i
 
for ((_i=0; _i<${#arr[*]}; _i++)); do
_ranked+=( "$(( _i + 1 )) ${arr[_i]}" )
done
}
 
# # Function _rankFractional(arr, rankarr) - retun arr with Fractional ranking
#
function _rankFractional {
typeset _ranked ; nameref _ranked="$1"
 
typeset _i _j _scr _currank _prevscr _shelf
integer _i _j _scr _prevscr
typeset -F1 _currank=1.0
typeset -a _shelf
 
for ((_i=0; _i<${#arr[*]}; _i++)); do
_scr=${arr[_i]%\ *}
if (( _i>0 )) && (( _scr != _prevscr )); then
(( _currank/=${#_shelf[*]} ))
for ((_j=0; _j<${#_shelf[*]}; _j++)); do
_ranked+=( "${_currank} ${_shelf[_j]}" )
done
_currank=0.0
unset _shelf ; typeset -a _shelf
fi
(( _i>0 )) && (( _currank+=_i + 1 ))
_shelf+=( "${arr[_i]}" )
_prevscr=${_scr}
done
for ((_j=0; _j<${#_shelf[*]}; _j++)); do
(( _currank/=${#_shelf[*]} ))
_ranked+=( "${_currank} ${_shelf[_j]}" )
done
}
 
######
# main #
######
 
printf "\n\nInput Data: ${#arr[*]} records\n---------------------\n"
for ((i=0; i< ${#arr[*]}; i++)); do
print ${arr[i]}
done
 
typeset -a rankedarr
_rankStandard rankedarr
printf "\n\nStandard Ranking\n----------------\n"
for ((i=0; i< ${#rankedarr[*]}; i++)); do
print ${rankedarr[i]}
done
 
unset rankedarr ; typeset -a rankedarr
_rankModified rankedarr
printf "\n\nModified Ranking\n----------------\n"
for ((i=0; i< ${#rankedarr[*]}; i++)); do
print ${rankedarr[i]}
done
 
unset rankedarr ; typeset -a rankedarr
_rankDense rankedarr
printf "\n\nDense Ranking\n-------------\n"
for ((i=0; i< ${#rankedarr[*]}; i++)); do
print ${rankedarr[i]}
done
 
unset rankedarr ; typeset -a rankedarr
_rankOrdinal rankedarr
printf "\n\nOrdinal Ranking\n---------------\n"
for ((i=0; i< ${#rankedarr[*]}; i++)); do
print ${rankedarr[i]}
done
 
unset rankedarr ; typeset -a rankedarr
_rankFractional rankedarr
printf "\n\nFractional Ranking\n------------------\n"
for ((i=0; i< ${#rankedarr[*]}; i++)); do
print ${rankedarr[i]}
done</syntaxhighlight>
{{out}}<pre>
Input Data: 7 records
---------------------
44 Solomon
42 Jason
42 Errol
41 Garry
41 Bernard
41 Barry
39 Stephen
 
Standard Ranking
----------------
1 44 Solomon
2 42 Jason
2 42 Errol
4 41 Garry
4 41 Bernard
4 41 Barry
7 39 Stephen
 
Modified Ranking
----------------
1 44 Solomon
3 42 Jason
3 42 Errol
6 41 Garry
6 41 Bernard
6 41 Barry
7 39 Stephen
 
Dense Ranking
-------------
1 44 Solomon
2 42 Jason
2 42 Errol
3 41 Garry
3 41 Bernard
3 41 Barry
4 39 Stephen
 
Ordinal Ranking
---------------
1 44 Solomon
2 42 Jason
3 42 Errol
4 41 Garry
5 41 Bernard
6 41 Barry
7 39 Stephen
 
Fractional Ranking
------------------
1.0 44 Solomon
2.5 42 Jason
2.5 42 Errol
5.0 41 Garry
5.0 41 Bernard
5.0 41 Barry
7.0 39 Stephen</pre>
 
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module Ranking (output$, orderlist) {
Open output$ for output as #k
Gosub getdata
Print #k, "Standard ranking:"
skip=true
rankval=1
oldrank=0
For i=1 to items
Read rank, name$
if skip then
skip=false
else.if oldrank<>rank then
rankval=i
end if
oldrank=rank
Print #k, Format$("{0::-5} {2} ({1})", rankval, rank, name$)
Next
Gosub getdata
Print #k, "Modified ranking:"
skip=true
rankval=Items
oldrank=0
ShiftBack 1, -items*2 ' reverse stack items
For i=items to 1
Read name$, rank
if skip then
skip=false
else.if oldrank<>rank then
rankval=i
end if
oldrank=rank
Data Format$("{0::-5} {2} ({1})", rankval, rank, name$)
Next
ShiftBack 1, -items ' reverse stack items
For i=1 to items
Print #k, letter$
Next i
Gosub getdata
Print #k, "Dense ranking:"
skip=true
Dense=Stack
acc=1
oldrank=0
For i=1 to items
Read rank, name$
if skip then
skip=false
oldrank=rank
else.if oldrank<>rank then
oldrank=rank
Gosub dense
acc=i
end if
Stack Dense {data Format$(" {0} ({1})",name$, rank)}
Next
Gosub dense
Gosub getdata
Print #k, "Ordinal ranking:"
For i=1 to items
Print #k, Format$("{0::-5} {2} ({1})", i, Number, letter$)
Next
Gosub getdata
Print #k, "Fractional ranking:"
skip=true
Frac=Stack
acc=1
oldrank=0
For i=1 to items
Read rank, name$
if skip then
skip=false
oldrank=rank
else.if oldrank<>rank then
oldrank=rank
Gosub Fractional
acc=I
end if
Stack Frac {data Format$(" {0} ({1})",name$, rank)}
Next
Gosub Fractional
Close #k
End
Fractional:
val=((len(Frac)+1)/2+(acc-1))
Stack Frac {
while not empty
Print #k, format$("{0:1:-5}{1}", val, letter$)
end while
}
Return
dense:
Stack Dense {
while not empty
Print #k, format$("{0::-5}{1}", acc, letter$)
end while
}
Return
getdata:
flush
stack stack(orderlist) // place a copy of items to current stack
items=stack.size/2
Return
}
Flush
Data 44, "Solomon", 42, "Jason", 42, "Errol"
Data 41, "Garry", 41, "Bernard", 41, "Barry"
Data 39, "Stephen"
// get all items from current stack to a new stack
alist=[]
// To screen
Ranking "", alist
// To file
Ranking "ranking.txt", alist
</syntaxhighlight>
{{out}}
<pre>
Standard ranking:
1 Solomon (44)
2 Jason (42)
2 Errol (42)
4 Garry (41)
4 Bernard (41)
4 Barry (41)
7 Stephen (39)
Modified ranking:
1 Solomon (44)
3 Jason (42)
3 Errol (42)
6 Garry (41)
6 Bernard (41)
6 Barry (41)
7 Stephen (39)
Dense ranking:
1 Solomon (44)
2 Jason (42)
2 Errol (42)
4 Garry (41)
4 Bernard (41)
4 Barry (41)
7 Stephen (39)
Ordinal ranking:
1 Solomon (44)
2 Jason (42)
3 Errol (42)
4 Garry (41)
5 Bernard (41)
6 Barry (41)
7 Stephen (39)
Fractional ranking:
1.0 Solomon (44)
2.5 Jason (42)
2.5 Errol (42)
5.0 Garry (41)
5.0 Bernard (41)
5.0 Barry (41)
7.0 Stephen (39)
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">data = Transpose@{{44, 42, 42, 41, 41, 41, 39}, {"Solomon", "Jason",
"Errol", "Garry", "Bernard", "Barry", "Stephen"}};
 
Line 2,933 ⟶ 3,683:
 
Grid@{fmtRankedData[data, #] & /@ {"standard", "modified", "dense",
"ordinal", "fractional"}}</syntaxhighlight>
 
</lang>
 
{{out}}
<pre>standard ranking:
standard ranking:
1 44 Solomon
3 42 Errol
Line 2,982 ⟶ 3,728:
5 41 Bernard
5 41 Garry
7 39 Stephen</pre>
</pre>
 
=={{header|Modula-2}}==
{{trans|C}}
<langsyntaxhighlight lang="modula2">MODULE RankingMethods;
FROM FormatString IMPORT FormatString;
FROM RealStr IMPORT RealToFixed;
Line 3,168 ⟶ 3,913:
 
ReadChar
END RankingMethods.</langsyntaxhighlight>
{{out}}
<pre>Ordinal Ranking
Line 3,223 ⟶ 3,968:
===Using an auxiliary table===
To simplify, it’s convenient to build a table giving for each score the list of competitor names.
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, stats, tables
 
type
Line 3,308 ⟶ 4,053:
echo "Fractional ranking:"
for (rank, name, score) in groups.fractionalRanks():
echo rank, ": ", name, " ", score</langsyntaxhighlight>
 
{{out}}
Line 3,358 ⟶ 4,103:
===Without an auxiliary table===
But it is possible to do the ranking without an auxiliary table.
<langsyntaxhighlight Nimlang="nim">import algorithm
 
type
Line 3,465 ⟶ 4,210:
echo "Fractional ranking:"
for (rank, name, score) in Data.fractionalRanks():
echo rank, ": ", name, " ", score</langsyntaxhighlight>
 
{{out}}
Line 3,473 ⟶ 4,218:
 
Replace "2" with "2.0" in <code>fractional</code> if you prefer decimal to fractional.
<langsyntaxhighlight lang="parigp">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;
Line 3,484 ⟶ 4,229:
dense(v)
ordinal(v)
fractional(v)</langsyntaxhighlight>
{{out}}
<pre>%1 = [[1, "Solomon"], [2, "Errol"], [2, "Jason"], [4, "Barry"], [4, "Bernard"], [4, "Garry"], [7, "Stephen"]]
Line 3,494 ⟶ 4,239:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">my %scores = (
'Solomon' => 44,
'Jason' => 42,
Line 3,561 ⟶ 4,306:
print "Dense:\n" . dense(%scores) . "\n";
print "Ordinal:\n" . ordinal(%scores) . "\n";
print "Fractional:\n" . fractional(%scores) . "\n";</langsyntaxhighlight>
{{out}}
<pre style="height:35ex">Standard:
Line 3,597 ⟶ 4,342:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function ties(sequence scores)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence t = {}, -- {start,num} pairs
<span style="color: #008080;">function</span> <span style="color: #000000;">ties</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">scores</span><span style="color: #0000FF;">)</span>
tdx = repeat(0,length(scores))
<span style="color: #004080;">sequence</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000080;font-style:italic;">-- {start,num} pairs</span>
integer last = -1
<span style="color: #000000;">tdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">scores</span><span style="color: #0000FF;">))</span>
for i=1 to length(scores) do
<span style="color: #004080;">integer</span> <span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
integer this = scores[i][1]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">scores</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if this=last then
<span style="color: #004080;">integer</span> <span style="color: #000000;">curr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">scores</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
t[$][2] += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">=</span><span style="color: #000000;">last</span> <span style="color: #008080;">then</span>
else
<span style="color: #000000;">t</span><span style="color: #0000FF;">[$][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
t = append(t,{i,1})
<span style="color: #008080;">else</span>
end if
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
tdx[i] = length(t)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
last = this
<span style="color: #000000;">tdx</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span>
-- eg {{{1,1},{2,2},{4,3},{7,1}},
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
-- {1,2,2,3,3,3,4}}
<span style="color: #000080;font-style:italic;">-- eg <nowiki>{{</nowiki>{1,1},{2,2},{4,3},{7,1<nowiki>}}</nowiki>,
return {t,tdx}
-- {1,2,2,3,3,3,4<nowiki>}}</nowiki></span>
end function
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tdx</span><span style="color: #0000FF;">}</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
enum STANDARD, -- eg {1,2,2,4,4,4,7}
MODIFIED, -- eg {1,3,3,6,6,6,7}
<span style="color: #008080;">enum</span> <span style="color: #000000;">STANDARD</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- eg {1,2,2,4,4,4,7}</span>
DENSE, -- (==tdx)
<span style="color: #000000;">MODIFIED</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- eg {1,3,3,6,6,6,7}</span>
ORDINAL, -- eg {1,2,3,4,5,6,7}
<span style="color: #000000;">DENSE</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (==tdx)</span>
FRACTION, -- {1,2.5,2.5,5,5,5,7}
<span style="color: #000000;">ORDINAL</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- eg {1,2,3,4,5,6,7}</span>
METHODS = $
<span style="color: #000000;">FRACTION</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- {1,2.5,2.5,5,5,5,7}</span>
 
<span style="color: #000000;">METHODS</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">$</span>
function rank(integer i, method, sequence t, tdx)
integer idx = tdx[i],
<span style="color: #008080;">function</span> <span style="color: #000000;">rank</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">method</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tdx</span><span style="color: #0000FF;">)</span>
{tx,tn} = t[idx]
<span style="color: #004080;">integer</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tdx</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span>
switch method
<span style="color: #0000FF;">{</span><span style="color: #000000;">tx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tn</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span>
case STANDARD: return tx
<span style="color: #008080;">switch</span> <span style="color: #000000;">method</span>
case MODIFIED: return tx+tn-1
<span style="color: #008080;">case</span> <span style="color: #000000;">STANDARD</span><span style="color: #0000FF;">:</span> <span style="color: #008080;">return</span> <span style="color: #000000;">tx</span>
case DENSE : return idx
<span style="color: #008080;">case</span> <span style="color: #000000;">MODIFIED</span><span style="color: #0000FF;">:</span> <span style="color: #008080;">return</span> <span style="color: #000000;">tx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">tn</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
case ORDINAL : return i
<span style="color: #008080;">case</span> <span style="color: #000000;">DENSE</span> <span style="color: #0000FF;">:</span> <span style="color: #008080;">return</span> <span style="color: #000000;">idx</span>
case FRACTION: return tx+(tn-1)/2
<span style="color: #008080;">case</span> <span style="color: #000000;">ORDINAL</span> <span style="color: #0000FF;">:</span> <span style="color: #008080;">return</span> <span style="color: #000000;">i</span>
end switch
<span style="color: #008080;">case</span> <span style="color: #000000;">FRACTION</span><span style="color: #0000FF;">:</span> <span style="color: #008080;">return</span> <span style="color: #000000;">tx</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">tn</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant scores = {{44, "Solomon"},
{42, "Jason"},
<span style="color: #008080;">constant</span> <span style="color: #000000;">scores</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">44</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Solomon"</span><span style="color: #0000FF;">},</span>
{42, "Errol"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">42</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Jason"</span><span style="color: #0000FF;">},</span>
{41, "Garry"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">42</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Errol"</span><span style="color: #0000FF;">},</span>
{41, "Bernard"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">41</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Garry"</span><span style="color: #0000FF;">},</span>
{41, "Barry"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">41</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Bernard"</span><span style="color: #0000FF;">},</span>
{39, "Stephen"}}
<span style="color: #0000FF;">{</span><span style="color: #000000;">41</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Barry"</span><span style="color: #0000FF;">},</span>
 
<span style="color: #0000FF;">{</span><span style="color: #000000;">39</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Stephen"</span><span style="color: #0000FF;">}}</span>
sequence {t,tdx} = ties(scores)
printf(1," score name standard modified dense ordinal fractional\n")
<span style="color: #004080;">sequence</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tdx</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ties</span><span style="color: #0000FF;">(</span><span style="color: #000000;">scores</span><span style="color: #0000FF;">)</span>
for i=1 to length(scores) do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" score name standard modified dense ordinal fractional\n"</span><span style="color: #0000FF;">)</span>
sequence ranks = repeat(0,METHODS)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">scores</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for method=1 to METHODS do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ranks</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">METHODS</span><span style="color: #0000FF;">)</span>
ranks[method] = rank(i,method,t,tdx)
<span style="color: #008080;">for</span> <span style="color: #000000;">method</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">METHODS</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">ranks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">method</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rank</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">method</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tdx</span><span style="color: #0000FF;">)</span>
printf(1,"%5d %-7s %6g %8g %6g %6g %9g\n",scores[i]&ranks)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%5d %-7s "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">scores</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%6g %8g %6g %6g %9g\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,665 ⟶ 4,414:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-Ranking
{
Line 3,808 ⟶ 4,557:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$scores = "44 Solomon","42 Jason","42 Errol","41 Garry","41 Bernard","41 Barry","39 Stephen"
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$scores | Get-Ranking -Standard
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,827 ⟶ 4,576:
Stephen 39 7
</pre>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$scores | Get-Ranking -Modified
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,842 ⟶ 4,591:
Stephen 39 7
</pre>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$scores | Get-Ranking -Dense
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,857 ⟶ 4,606:
Stephen 39 4
</pre>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$scores | Get-Ranking -Ordinal
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,872 ⟶ 4,621:
Stephen 39 7
</pre>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$scores | Get-Ranking -Fractional
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,889 ⟶ 4,638:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def mc_rank(iterable, start=1):
"""Modified competition ranking"""
lastresult, fifo = None, []
Line 3,962 ⟶ 4,711:
print('\n%s:' % ranker.__doc__)
for rank, score in ranker(scores):
print(' %3g, %r' % (rank, score))</langsyntaxhighlight>
 
{{out}}
Line 4,020 ⟶ 4,769:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
;; Tim-brown 2014-09-11
 
Line 4,096 ⟶ 4,845:
(caddr r)
(cdddr r)))
(newline))</langsyntaxhighlight>
 
{{out}}
Line 4,146 ⟶ 4,895:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my @scores =
Solomon => 44,
Jason => 42,
Line 4,190 ⟶ 4,939:
say "\nDense:"; .perl.say for dense @scores;
say "\nOrdinal:"; .perl.say for ordinal @scores;
say "\nFractional:"; .perl.say for fractional @scores;</langsyntaxhighlight>
{{out}}
<pre>Standard:
Line 4,226 ⟶ 4,975:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/**************************
44 Solomon 1 1 1 1 1
42 Jason 2 3 2 2 2.5
Line 4,274 ⟶ 5,023:
cp=p
End
Say cnt.ok 'correct lines'</langsyntaxhighlight>
{{out}}
<pre>44 Solomon 1 1 1 1 1
Line 4,286 ⟶ 5,035:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">ar = "44 Solomon
42 Jason
42 Errol
Line 4,306 ⟶ 5,055:
end
s_rnk += names.size
end</langsyntaxhighlight>
{{out}}
<pre>
Line 4,321 ⟶ 5,070:
=={{header|Scala}}==
This example uses a type-safe singly-linked object model with no mutable state variables, which makes it longer than the Ruby version above, but demonstrates an object-oriented functional programming approach.
<langsyntaxhighlight Scalalang="scala">object RankingMethods extends App {
case class Score(score: Int, name: String) // incoming data
case class Rank[Precision](rank: Precision, names: List[String]) // outgoing results (can be int or double)
Line 4,364 ⟶ 5,113:
println(rankFractional(test) mkString "\n")
 
}</langsyntaxhighlight>
{{out}}
<pre>Standard:
Line 4,401 ⟶ 5,150:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var scores = [
Pair(Solomon => 44),
Pair(Jason => 42),
Line 4,462 ⟶ 5,211:
say "\nDense:"; display( dense(scores))
say "\nOrdinal:"; display( ordinal(scores))
say "\nFractional:"; display(fractional(scores))</langsyntaxhighlight>
{{out}}
<pre>
Line 4,500 ⟶ 5,249:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc rank {rankingMethod sortedList} {
# Extract the groups in the data (this is pointless for ordinal...)
set s [set group [set groups {}]]
Line 4,555 ⟶ 5,304:
puts " $rank\t$score\t$who"
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,604 ⟶ 5,353:
7 39 Stephen
</pre>
 
 
=={{header|True BASIC}}==
{{trans|BASIC}}
<syntaxhighlight lang="basic">
LET n = 7
DIM puntos(7), ptosnom(7), nombre$(7)
 
SUB MostarTabla
FOR i = 1 to n
PRINT str$(ptosnom(i)); " "; puntos(i); " "; nombre$(i)
NEXT i
PRINT
END SUB
 
PRINT "Puntuaciones a clasificar (mejores primero):"
FOR i = 1 to n
READ puntos(i), nombre$(i)
PRINT " "; puntos(i); " "; nombre$(i)
NEXT i
 
PRINT
PRINT "--- Standard ranking ---"
LET ptosnom(1) = 1
FOR i = 2 to n
NEXT i
CALL MostarTabla
 
PRINT "--- Modified ranking ---"
LET ptosnom(n) = n
FOR i = n-1 to 1 step -1
IF puntos(i) = puntos(i+1) then LET ptosnom(i) = ptosnom(i+1) else LET ptosnom(i) = i
NEXT i
CALL MostarTabla
 
PRINT "--- Ordinal ranking ---"
FOR i = 1 to n
LET ptosnom(i) = i
NEXT i
CALL MostarTabla
 
PRINT "--- Fractional ranking ---"
LET i = 1
LET j = 2
DO
IF j <= n then
IF (puntos(j-1) = puntos(j)) then
LET j = j + 1
END IF
END IF
 
FOR k = i to j-1
LET ptosnom(k) = (i+j-1) / 2
NEXT k
LET i = j
LET j = j + 1
LOOP UNTIL i > n
CALL MOSTARTABLA
 
DATA 44, "Solomon", 42, "Jason", 42, "Errol", 41, "Garry", 41, "Bernard", 41, "Barry", 39, "Stephen"
END
</syntaxhighlight>
 
 
=={{header|Visual FoxPro}}==
<langsyntaxhighlight lang="vfp">
#DEFINE CTAB CHR(9)
#DEFINE CRLF CHR(13) + CHR(10)
Line 4,709 ⟶ 5,521:
ENDFOR
ENDPROC
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,726 ⟶ 5,538:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Nums
import "./fmt" for Fmt
 
/* all ranking functions assume the array of Pairs is non-empty and already sorted
Line 4,807 ⟶ 5,619:
printRankings.call("Dense ranking", denseRanking.call(scores), scores)
printRankings.call("Ordinal ranking", ordinalRanking.call(scores), scores)
printFractionalRankings.call("Fractional ranking", fractionalRanking.call(scores), scores)</langsyntaxhighlight>
 
{{out}}
Line 4,856 ⟶ 5,668:
7.00 39 Stephen
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">
n = 7
dim puntos(7), ptosnom(7), nombre$(7)
 
sub MostarTabla()
for i = 1 to n
print str$(ptosnom(i)), " ", puntos(i), " ", nombre$(i)
next i
print
end sub
 
print "Puntuaciones a clasificar (mejores primero):"
for i = 1 to n
read puntos(i), nombre$(i)
print " ", puntos(i), " ", nombre$(i)
next i
 
print
print "--- Standard ranking ---"
ptosnom(1) = 1
for i = 2 to n
if puntos(i) = puntos(i-1) then ptosnom(i) = ptosnom(i-1) else ptosnom(i) = i : fi
next i
MostarTabla()
 
print "--- Modified ranking ---"
ptosnom(n) = n
for i = n-1 to 1 step -1
if puntos(i) = puntos(i+1) then ptosnom(i) = ptosnom(i+1) else ptosnom(i) = i : fi
next i
MostarTabla()
 
print "--- Ordinal ranking ---"
for i = 1 to n
ptosnom(i) = i
next i
MostarTabla()
 
print "--- Fractional ranking ---"
i = 1
j = 2
repeat
if j <= n then
if (puntos(j-1) = puntos(j)) then j = j + 1 : fi
end if
for k = i to j-1
ptosnom(k) = (i+j-1) / 2
next k
i = j
j = j + 1
until i > n
MostarTabla()
 
data 44, "Solomon", 42, "Jason", 42, "Errol", 41, "Garry", 41, "Bernard", 41, "Barry", 39, "Stephen"
end
</syntaxhighlight>
 
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn group(scores){ // group like scores into one list --> list of lists
sink:=List();
scores.reduce('wrap(ps,sn,buf){
Line 4,891 ⟶ 5,763:
print(group,"%5.2f".fmt(r));
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl"> // these are sorted!?!
scores:=T(T(44,"Solomon"), T(42,"Jason"), T(42,"Errol"), T(41,"Garry"),
T(41,"Bernard"),T(41,"Barry"),T(39,"Stephen"),);
Line 4,899 ⟶ 5,771:
"Dense:" .println(); rankViaDense(scores);
"Ordinal:" .println(); rankViaOrdinal(scores);
"Fractional:".println(); rankViaFractional(scores);</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits