Ranking methods: Difference between revisions

m
(Added Wren)
m (→‎{{header|Wren}}: Minor tidy)
 
(17 intermediate revisions by 11 users not shown)
Line 1:
{{task|Sorting Algorithms}}
[[Category:Sorting]]
{{Sorting Algorithm}}
 
The numerical rank of competitors in a competition shows if one is better than, equal to, or worse than another based on their results in a competition.
 
Line 28 ⟶ 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 52 ⟶ 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 83 ⟶ 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 98 ⟶ 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 104 ⟶ 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 112 ⟶ 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 120 ⟶ 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 140 ⟶ 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 211 ⟶ 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 235 ⟶ 584:
}
return Res%opt%
}</langsyntaxhighlight>
Example:<langsyntaxhighlight AutoHotkeylang="autohotkey">data =
(
44 Solomon
Line 253 ⟶ 602:
. "`nOrdinal Ranking:`n" Rank(data, 4)
. "`nFractional Ranking:`n" Rank(data, 5)
return</langsyntaxhighlight>
Output:<pre>Standard Ranking:
1 44 Solomon
Line 306 ⟶ 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 372 ⟶ 721:
{{trans|Python}}
This uses separate files for each method of ranking:
<langsyntaxhighlight lang="awk">##
## Dense ranking in file: ranking_d.awk
##
Line 472 ⟶ 821:
}
//{sc_rank()}
</syntaxhighlight>
</lang>
 
The input as a file <code>ranking.txt</code>:
Line 532 ⟶ 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 581 ⟶ 930:
550 DATA 41,Bernard
560 DATA 41,Barry
570 DATA 39,Stephen</langsyntaxhighlight>
 
{{out}}
Line 632 ⟶ 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 750 ⟶ 1,099:
return 0;
}
</syntaxhighlight>
</lang>
Input file, first row is number of records :
<pre>
Line 820 ⟶ 1,169:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 956 ⟶ 1,305:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Standard Rank
Line 1,005 ⟶ 1,354:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iomanip>
#include <iostream>
Line 1,180 ⟶ 1,529:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Standard Rank
Line 1,228 ⟶ 1,577:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
# List of competitors
Line 1,357 ⟶ 1,706:
print_nl();
n := n + 1;
end loop;</langsyntaxhighlight>
 
{{out}}
Line 1,407 ⟶ 1,756:
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import std.algorithm;
import std.stdio;
 
Line 1,573 ⟶ 1,922:
 
writeln;
}</langsyntaxhighlight>
 
{{out}}
Line 1,623 ⟶ 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,655 ⟶ 2,004:
|> Enum.chunk(2)
|> Enum.map(fn [score,name] -> {String.to_integer(score),name} end)
|> Ranking.methods</langsyntaxhighlight>
 
{{out}}
Line 1,671 ⟶ 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,706 ⟶ 2,055:
[ [ print ] [ .rank nl ] bi* ] 2 5 mnapply ;
 
MAIN: ranking-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 1,754 ⟶ 2,103:
7 39 Stephen
</pre>
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
Data 44,"Solomon", 42,"Jason", 42,"Errol", 41,"Garry"
Data 41,"Bernard", 41,"Barry", 39,"Stephen"
 
Dim Shared As Integer n = 7
Dim Shared As Integer puntos(n), i
Dim Shared As Single ptosnom(n)
Dim Shared As String nombre(n)
 
Print "Puntuaciones a clasificar (mejores primero):"
For i = 1 To n
Read puntos(i), nombre(i)
Print Using " ##, \ \"; puntos(i); nombre(i)
Next i
Print
 
Sub MostarTabla
For i = 1 To n
Print Using " \ \ ##, \ \"; Str(ptosnom(i)); puntos(i); nombre(i)
Next i
Print
End Sub
 
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
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
Next i
MostarTabla
 
Print "--- Dense ranking ---"
ptosnom(1) = 1
For i = 2 To n
ptosnom(i) = ptosnom(i-1) - (puntos(i) <> puntos(i-1))
Next i
MostarTabla
 
Print "--- Ordinal ranking ---"
For i = 1 To n
ptosnom(i) = i
Next i
MostarTabla
 
Print "--- Fractional ranking ---"
i = 1
Dim As Integer j = 2
Do
If j <= n Then If (puntos(j-1) = puntos(j)) Then j += 1
For k As Integer = i To j-1
ptosnom(k) = (i+j-1) / 2
Next k
i = j
j += 1
Loop While i <= n
MostarTabla
Sleep
</syntaxhighlight>
{{out}}
<pre>
Puntuaciones a clasificar (mejores primero):
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 44 Solomon
2.5 42 Jason
2.5 42 Errol
4.5 41 Garry
4.5 41 Bernard
6 41 Barry
7 39 Stephen
</pre>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,868 ⟶ 2,342:
show("\nOrdinal", OrdinalRank)
show("\nFractional", FractionalRank)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,918 ⟶ 2,392:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.List (groupBy, sortBy, intercalate)
 
type Item = (Int, String)
Line 2,006 ⟶ 2,480:
nicePrint "Dense:" $ dense test
nicePrint "Ordinal:" $ ordinal test
nicePrint "Fractional:" $ fractional test</langsyntaxhighlight>
{{Out}}
<pre>Standard:
Line 2,056 ⟶ 2,530:
Implementation:
 
<langsyntaxhighlight Jlang="j">competitors=:<;._1;._2]0 :0
44 Solomon
42 Jason
Line 2,074 ⟶ 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,080 ⟶ 2,554:
Task examples:
 
<langsyntaxhighlight Jlang="j"> standard rank competitors
┌─┬──┬───────┐
│1│44│Solomon│
Line 2,159 ⟶ 2,633:
├───┼──┼───────┤
│7 │39│Stephen│
└───┴──┴───────┘</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.util.*;
 
public class RankingMethods {
Line 2,237 ⟶ 2,711:
}
}
}</langsyntaxhighlight>
 
<pre>Standard ranking
Line 2,292 ⟶ 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,367 ⟶ 2,841:
return wikiTable(tbl, true, 'text-align:center');
})();</langsyntaxhighlight>
 
{{out}}
Line 2,392 ⟶ 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,450 ⟶ 2,924:
 
return wikiTable(tbl, true, 'text-align:center');
}))();</langsyntaxhighlight>
 
{| class="wikitable" style="text-align:center"
Line 2,479 ⟶ 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,536 ⟶ 3,010:
else [ resolve, [ $i + 1 ] ]
end )
| resolve ;</langsyntaxhighlight>Task<syntaxhighlight lang ="jq">def raw:
[
"Solomon", 44,
Line 2,555 ⟶ 3,029:
task
</syntaxhighlight>
</lang>
{{Out}}
standard: [1,2,2,4,4,4,7]
Line 2,565 ⟶ 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,582 ⟶ 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,616 ⟶ 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,641 ⟶ 3,115:
println()
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,656 ⟶ 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,735 ⟶ 3,209:
printRankings("Ordinal ranking", ordinalRanking(scores), scores)
printFractionalRankings("Fractional ranking", fractionalRanking(scores), scores)
}</langsyntaxhighlight>
 
{{out}}
Line 2,785 ⟶ 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,805 ⟶ 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,854 ⟶ 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,040 ⟶ 3,913:
 
ReadChar
END RankingMethods.</langsyntaxhighlight>
{{out}}
<pre>Ordinal Ranking
Line 3,091 ⟶ 3,964:
5.0 41 Barry
7.0 39 Stephen</pre>
 
=={{header|Nim}}==
===Using an auxiliary table===
To simplify, it’s convenient to build a table giving for each score the list of competitor names.
<syntaxhighlight lang="nim">import algorithm, sequtils, stats, tables
 
type
Record = tuple[score: int; name: string] # Input data.
Groups = OrderedTable[int, seq[string]] # Maps score to list of names.
Rank = tuple[rank: int; name: string; score: int] # Result.
FractRank = tuple[rank: float; name: string; score: int] # Result (fractional).
 
func cmp(a, b: (int, seq[string])): int =
## Comparison function needed to sort the groups.
cmp(a[0], b[0])
 
func toGroups(records: openArray[Record]): Groups =
## Build a "Groups" table from the records.
for record in records:
result.mgetOrPut(record.score, @[]).add record.name
# Sort the list of names by alphabetic order.
for score in result.keys:
sort(result[score])
# Sort the groups by decreasing score.
result.sort(cmp, Descending)
 
func standardRanks(groups: Groups): seq[Rank] =
var rank = 1
for score, names in groups.pairs:
for name in names:
result.add (rank, name, score)
inc rank, names.len
 
func modifiedRanks(groups: Groups): seq[Rank] =
var rank = 0
for score, names in groups.pairs:
inc rank, names.len
for name in names:
result.add (rank, name, score)
 
func denseRanks(groups: Groups): seq[Rank] =
var rank = 0
for score, names in groups.pairs:
inc rank
for name in names:
result.add (rank, name, score)
 
func ordinalRanks(groups: Groups): seq[Rank] =
var rank = 0
for score, names in groups.pairs:
for name in names:
inc rank
result.add (rank, name, score)
 
func fractionalRanks(groups: Groups): seq[FractRank] =
var rank = 1
for score, names in groups.pairs:
let fRank = mean(toSeq(rank..(rank + names.high)))
for name in names:
result.add (fRank, name, score)
inc rank, names.len
 
when isMainModule:
const Data = [(44, "Solomon"), (42, "Jason"), (42, "Errol"),
(41, "Garry"), (41, "Bernard"), (41, "Barry"), (39, "Stephen")]
 
let groups = Data.toGroups()
echo "Standard ranking:"
for (rank, name, score) in groups.standardRanks():
echo rank, ": ", name, " ", score
 
echo()
echo "Modified ranking:"
for (rank, name, score) in groups.modifiedRanks():
echo rank, ": ", name, " ", score
 
echo()
echo "Dense ranking:"
for (rank, name, score) in groups.denseRanks():
echo rank, ": ", name, " ", score
 
echo()
echo "Ordinal ranking:"
for (rank, name, score) in groups.ordinalRanks():
echo rank, ": ", name, " ", score
 
echo()
echo "Fractional ranking:"
for (rank, name, score) in groups.fractionalRanks():
echo rank, ": ", name, " ", score</syntaxhighlight>
 
{{out}}
<pre>Standard ranking:
1: Solomon 44
2: Errol 42
2: Jason 42
4: Barry 41
4: Bernard 41
4: Garry 41
7: Stephen 39
 
Modified ranking:
1: Solomon 44
3: Errol 42
3: Jason 42
6: Barry 41
6: Bernard 41
6: Garry 41
7: Stephen 39
 
Dense ranking:
1: Solomon 44
2: Errol 42
2: Jason 42
3: Barry 41
3: Bernard 41
3: Garry 41
4: Stephen 39
 
Ordinal ranking:
1: Solomon 44
2: Errol 42
3: Jason 42
4: Barry 41
5: Bernard 41
6: Garry 41
7: Stephen 39
 
Fractional ranking:
1.0: Solomon 44
2.5: Errol 42
2.5: Jason 42
5.0: Barry 41
5.0: Bernard 41
5.0: Garry 41
7.0: Stephen 39</pre>
 
===Without an auxiliary table===
But it is possible to do the ranking without an auxiliary table.
<syntaxhighlight lang="nim">import algorithm
 
type
Record = tuple[score: int; name: string] # Input data.
Rank = tuple[rank: int; name: string; score: int] # Result.
FractRank = tuple[rank: float; name: string; score: int] # Result (fractional).
 
func cmp(a, b: Record): int =
## Record comparison function (needed for sorting).
result = cmp(b[0], a[0]) # Reverse order.
if result == 0:
result = cmp(a.name, b.name) # Alphabetical order.
 
func standardRanks(records: openArray[Record]): seq[Rank] =
let records = sorted(records, cmp)
var rank = 1
var currScore = records[0].score
for idx, (score, name) in records:
if score != currScore:
rank = idx + 1
currScore = score
result.add (rank, name, score)
 
func modifiedRanks(records: openArray[Record]): seq[Rank] =
let records = sorted(records, cmp)
var rank = records.len
var currScore = records[^1].score
for idx in countdown(records.high, 0):
let (score, name) = records[idx]
if score != currScore:
rank = idx + 1
currScore = score
result.add (rank, name, score)
result.reverse()
 
func denseRanks(records: openArray[Record]): seq[Rank] =
let records = sorted(records, cmp)
var rank = 1
var currScore = records[0].score
for (score, name) in records:
if score != currScore:
inc rank
currScore = score
result.add (rank, name, score)
 
func ordinalRanks(records: openArray[Record]): seq[Rank] =
let records = sorted(records, cmp)
var rank = 0
for (score, name) in records:
inc rank
result.add (rank, name, score)
 
func fractionalRanks(records: openArray[Record]): seq[FractRank] =
let records = sorted(records, cmp)
# Build a list of ranks.
var currScore = records[0].score
var sum = 0
var ranks: seq[float]
var count = 0
for idx, record in records:
if record.score == currScore:
inc count
inc sum, idx + 1
else:
ranks.add sum / count
count = 1
currScore = record.score
sum = idx + 1
ranks.add sum / count
 
# Give a rank to each record.
currScore = records[0].score
var rankIndex = 0
for (score, name) in records:
if score != currScore:
inc rankIndex
currScore = score
result.add (ranks[rankIndex], name, score)
 
when isMainModule:
 
const Data = [(44, "Solomon"), (42, "Jason"), (42, "Errol"),
(41, "Garry"), (41, "Bernard"), (41, "Barry"), (39, "Stephen")]
 
echo "Standard ranking:"
for (rank, name, score) in Data.standardRanks():
echo rank, ": ", name, " ", score
 
echo()
echo "Modified ranking:"
for (rank, name, score) in Data.modifiedRanks():
echo rank, ": ", name, " ", score
 
echo()
echo "Dense ranking:"
for (rank, name, score) in Data.denseRanks():
echo rank, ": ", name, " ", score
 
echo()
echo "Ordinal ranking:"
for (rank, name, score) in Data.ordinalRanks():
echo rank, ": ", name, " ", score
 
echo()
echo "Fractional ranking:"
for (rank, name, score) in Data.fractionalRanks():
echo rank, ": ", name, " ", score</syntaxhighlight>
 
{{out}}
<pre>Same output as with other version.</pre>
 
=={{header|PARI/GP}}==
 
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,106 ⟶ 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,116 ⟶ 4,239:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">my %scores = (
'Solomon' => 44,
'Jason' => 42,
Line 3,183 ⟶ 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,219 ⟶ 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,287 ⟶ 4,414:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-Ranking
{
Line 3,430 ⟶ 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,449 ⟶ 4,576:
Stephen 39 7
</pre>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$scores | Get-Ranking -Modified
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,464 ⟶ 4,591:
Stephen 39 7
</pre>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$scores | Get-Ranking -Dense
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,479 ⟶ 4,606:
Stephen 39 4
</pre>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$scores | Get-Ranking -Ordinal
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,494 ⟶ 4,621:
Stephen 39 7
</pre>
<syntaxhighlight lang="powershell">
<lang PowerShell>
$scores | Get-Ranking -Fractional
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,511 ⟶ 4,638:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def mc_rank(iterable, start=1):
"""Modified competition ranking"""
lastresult, fifo = None, []
Line 3,584 ⟶ 4,711:
print('\n%s:' % ranker.__doc__)
for rank, score in ranker(scores):
print(' %3g, %r' % (rank, score))</langsyntaxhighlight>
 
{{out}}
Line 3,642 ⟶ 4,769:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
;; Tim-brown 2014-09-11
 
Line 3,718 ⟶ 4,845:
(caddr r)
(cdddr r)))
(newline))</langsyntaxhighlight>
 
{{out}}
Line 3,768 ⟶ 4,895:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my @scores =
Solomon => 44,
Jason => 42,
Line 3,812 ⟶ 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 3,848 ⟶ 4,975:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/**************************
44 Solomon 1 1 1 1 1
42 Jason 2 3 2 2 2.5
Line 3,896 ⟶ 5,023:
cp=p
End
Say cnt.ok 'correct lines'</langsyntaxhighlight>
{{out}}
<pre>44 Solomon 1 1 1 1 1
Line 3,908 ⟶ 5,035:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">ar = "44 Solomon
42 Jason
42 Errol
Line 3,928 ⟶ 5,055:
end
s_rnk += names.size
end</langsyntaxhighlight>
{{out}}
<pre>
Line 3,943 ⟶ 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 3,986 ⟶ 5,113:
println(rankFractional(test) mkString "\n")
 
}</langsyntaxhighlight>
{{out}}
<pre>Standard:
Line 4,023 ⟶ 5,150:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var scores = [
Pair(Solomon => 44),
Pair(Jason => 42),
Line 4,084 ⟶ 5,211:
say "\nDense:"; display( dense(scores))
say "\nOrdinal:"; display( ordinal(scores))
say "\nFractional:"; display(fractional(scores))</langsyntaxhighlight>
{{out}}
<pre>
Line 4,122 ⟶ 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,177 ⟶ 5,304:
puts " $rank\t$score\t$who"
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,226 ⟶ 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,331 ⟶ 5,521:
ENDFOR
ENDPROC
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,348 ⟶ 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,429 ⟶ 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,478 ⟶ 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,513 ⟶ 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,521 ⟶ 5,771:
"Dense:" .println(); rankViaDense(scores);
"Ordinal:" .println(); rankViaOrdinal(scores);
"Fractional:".println(); rankViaFractional(scores);</langsyntaxhighlight>
{{out}}
<pre>
9,487

edits