Ranking methods: Difference between revisions
m
→{{header|Wren}}: Minor tidy
(Ranking methods en FreeBASIC) |
m (→{{header|Wren}}: Minor tidy) |
||
(13 intermediate revisions by 9 users not shown) | |||
Line 31:
<br><br>
=={{header|
{{trans|Python}}
<syntaxhighlight lang="11l">F mc_rank([(Int, String)] iterable)
‘Modified competition ranking’
[(Float, (Int, String))] r
V lastresult = -1
[(Int, String)] fifo
L(item) iterable
I item[0] == lastresult
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 '
-- 'me' and 'my'
property results : missing value
property startIndex : 1
property endIndex : a reference to
property step : 1
property currentRank : missing value
Line 55 ⟶ 407:
on resultsFrom(theScores)
copy theScores to my results
tell sorter to sort(my results, my startIndex, my endIndex, {comparer:me})
set my currentScore to
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
set my results to missing value
return
end resultsFrom
-- Comparison handler used by the sort
on isGreater(a, b)
if (a's score
return ((a's score
end isGreater
--
on rankResult(i)
set thisResult to my results's item i
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
end rankResult
end script
script modifiedRanking
property parent : standardRanking
property startIndex : a reference to
property endIndex : 1
property step : -1
Line 101 ⟶ 447:
on rankResult(i)
set thisResult to my results's item i
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
end rankResult
end script
Line 115 ⟶ 461:
on rankResult(i)
set my results's item i
end rankResult
end script
Line 123 ⟶ 469:
on rankResult(i)
set thisResult to my results's item i
set thisScore to thisResult's score
if (thisScore is not my currentScore) then
-- 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
end repeat
set my currentRank to i
set my currentScore to thisScore
end if
set my results's item i
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 & ")"
end repeat
return
end formatRankings
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to
set
set AppleScript's text item delimiters to astid
return txt
end join
local theScores, output
set theScores to {{score:44, competitor:"Solomon"}, {score:42, competitor:"Jason
{score:
{score:41, competitor:"Barry"}, {score:39, competitor:"Stephen"}}
set output to {
}
return join(output, linefeed & linefeed)</syntaxhighlight>
{{output}}
<
1 Solomon (44)
2 Errol (42)
Line 214 ⟶ 560:
5.0 Bernard (41)
5.0 Garry (41)
7.0 Stephen (39)"</
=={{header|AutoHotkey}}==
<
for index, val in StrSplit(data, "`n", "`r") {
RegExMatch(val, "^(\d+)\s+(.*)", Match)
Line 238 ⟶ 584:
}
return Res%opt%
}</
Example:<
(
44 Solomon
Line 256 ⟶ 602:
. "`nOrdinal Ranking:`n" Rank(data, 4)
. "`nFractional Ranking:`n" Rank(data, 5)
return</
Output:<pre>Standard Ranking:
1 44 Solomon
Line 309 ⟶ 655:
matrix, with the ranking added.
<
modified ← ∊∘(⌈\∘⌽¨⊢⊂⍳∘≢)∘(1,2≠/⊢)∘(1⌷[2]⊢),⊢
dense ← (+\1,2≠/1⌷[2]⊢),⊢
ordinal ← ⍳∘≢,⊢
fractional ← ∊∘((≢(/∘⊢)+/÷≢)¨⊢⊂⍳∘≢)∘(1,2≠/⊢)∘(1⌷[2]⊢),⊢</
{{out}}
Line 375 ⟶ 721:
{{trans|Python}}
This uses separate files for each method of ranking:
<
## Dense ranking in file: ranking_d.awk
##
Line 475 ⟶ 821:
}
//{sc_rank()}
</syntaxhighlight>
The input as a file <code>ranking.txt</code>:
Line 535 ⟶ 881:
=={{header|BASIC}}==
<
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</
{{out}}
Line 635 ⟶ 981:
=={{header|C}}==
Takes the scores as input via a file, prints out usage on incorrect invocation.
<syntaxhighlight lang="c">
#include<stdlib.h>
#include<stdio.h>
Line 753 ⟶ 1,099:
return 0;
}
</syntaxhighlight>
Input file, first row is number of records :
<pre>
Line 823 ⟶ 1,169:
=={{header|C sharp|C#}}==
{{trans|D}}
<
using System.Collections.Generic;
using System.Linq;
Line 959 ⟶ 1,305:
}
}
}</
{{out}}
<pre>Standard Rank
Line 1,008 ⟶ 1,354:
=={{header|C++}}==
{{trans|C#}}
<
#include <iomanip>
#include <iostream>
Line 1,183 ⟶ 1,529:
return 0;
}</
{{out}}
<pre>Standard Rank
Line 1,231 ⟶ 1,577:
=={{header|Cowgol}}==
<
# List of competitors
Line 1,360 ⟶ 1,706:
print_nl();
n := n + 1;
end loop;</
{{out}}
Line 1,410 ⟶ 1,756:
=={{header|D}}==
<
import std.stdio;
Line 1,576 ⟶ 1,922:
writeln;
}</
{{out}}
Line 1,626 ⟶ 1,972:
=={{header|Elixir}}==
{{trans|Ruby}}
<
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</
{{out}}
Line 1,674 ⟶ 2,020:
=={{header|Factor}}==
{{works with|Factor|0.99 2019-07-10}}
<
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</
{{out}}
<pre>
Line 1,760 ⟶ 2,106:
=={{header|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>
{{out}}
<pre>
Line 1,884 ⟶ 2,230:
=={{header|Go}}==
<
import (
Line 1,996 ⟶ 2,342:
show("\nOrdinal", OrdinalRank)
show("\nFractional", FractionalRank)
}</
{{out}}
<pre>
Line 2,046 ⟶ 2,392:
=={{header|Haskell}}==
<
type Item = (Int, String)
Line 2,134 ⟶ 2,480:
nicePrint "Dense:" $ dense test
nicePrint "Ordinal:" $ ordinal test
nicePrint "Fractional:" $ fractional test</
{{Out}}
<pre>Standard:
Line 2,184 ⟶ 2,530:
Implementation:
<
44 Solomon
42 Jason
Line 2,202 ⟶ 2,548:
fractional=: #/.~ # ] (+/%#)/. #\
rank=:1 :'<"0@u@:scores,.]'</
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:
<
┌─┬──┬───────┐
│1│44│Solomon│
Line 2,287 ⟶ 2,633:
├───┼──┼───────┤
│7 │39│Stephen│
└───┴──┴───────┘</
=={{header|Java}}==
{{works with|Java|8}}
<
public class RankingMethods {
Line 2,365 ⟶ 2,711:
}
}
}</
<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)
<
var xs = 'Solomon Jason Errol Garry Bernard Barry Stephen'.split(' '),
Line 2,495 ⟶ 2,841:
return wikiTable(tbl, true, 'text-align:center');
})();</
{{out}}
Line 2,520 ⟶ 2,866:
===ES6===
<
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');
}))();</
{| class="wikitable" style="text-align:center"
Line 2,607 ⟶ 2,953:
For the sake of brevity, only the ranks are printed.
<syntaxhighlight 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 ;</
[
"Solomon", 44,
Line 2,683 ⟶ 3,029:
task
</syntaxhighlight>
{{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">
function ties{T<:Real}(a::Array{T,1})
unique(a[2:end][a[2:end] .== a[1:end-1]])
end
</syntaxhighlight>
<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">
function rankstandard{T<:Real}(a::Array{T,1})
r = collect(1:length(a))
Line 2,710 ⟶ 3,056:
return r
end
</syntaxhighlight>
'''Modified Ranking Function'''
<syntaxhighlight lang="julia">
function rankmodified{T<:Real}(a::Array{T,1})
indexin(a, a)
end
</syntaxhighlight>
'''Dense Ranking Function'''
<syntaxhighlight lang="julia">
function rankdense{T<:Real}(a::Array{T,1})
indexin(a, unique(a))
end
</syntaxhighlight>
'''Ordinal Ranking Function'''
<syntaxhighlight lang="julia">
function rankordinal{T<:Real}(a::Array{T,1})
collect(1:length(a))
end
</syntaxhighlight>
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">
function rankfractional{T<:Real}(a::Array{T,1})
r = float64(collect(1:length(a)))
Line 2,744 ⟶ 3,090:
return r
end
</syntaxhighlight>
'''Main'''
<syntaxhighlight lang="julia">
scores = [44, 42, 42, 41, 41, 41, 39]
names = ["Solomon", "Jason", "Errol", "Garry",
Line 2,769 ⟶ 3,115:
println()
end
</syntaxhighlight>
{{out}}
Line 2,784 ⟶ 3,130:
=={{header|Kotlin}}==
<
/* 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)
}</
{{out}}
Line 2,913 ⟶ 3,259:
</pre>
=={{header|
<syntaxhighlight lang="ksh">
#!/bin/ksh
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>
{{out}}
<pre>standard ranking:
1 44 Solomon
3 42 Errol
Line 2,982 ⟶ 3,728:
5 41 Bernard
5 41 Garry
7 39 Stephen</pre>
=={{header|Modula-2}}==
{{trans|C}}
<
FROM FormatString IMPORT FormatString;
FROM RealStr IMPORT RealToFixed;
Line 3,168 ⟶ 3,913:
ReadChar
END RankingMethods.</
{{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.
<
type
Line 3,308 ⟶ 4,053:
echo "Fractional ranking:"
for (rank, name, score) in groups.fractionalRanks():
echo rank, ": ", name, " ", score</
{{out}}
Line 3,358 ⟶ 4,103:
===Without an auxiliary table===
But it is possible to do the ranking without an auxiliary table.
<
type
Line 3,465 ⟶ 4,210:
echo "Fractional ranking:"
for (rank, name, score) in Data.fractionalRanks():
echo rank, ": ", name, " ", score</
{{out}}
Line 3,473 ⟶ 4,218:
Replace "2" with "2.0" in <code>fractional</code> if you prefer decimal to fractional.
<
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)</
{{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}}
<
'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";</
{{out}}
<pre style="height:35ex">Standard:
Line 3,597 ⟶ 4,342:
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">else</span>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<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>
<span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">-- eg <nowiki>{{</nowiki>{1,1},{2,2},{4,3},{7,1<nowiki>}}</nowiki>,
-- {1,2,2,3,3,3,4<nowiki>}}</nowiki></span>
<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>
<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>
<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>
<span style="color: #000000;">DENSE</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (==tdx)</span>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">switch</span> <span style="color: #000000;">method</span>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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;">"%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">
function Get-Ranking
{
Line 3,808 ⟶ 4,557:
}
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
$scores = "44 Solomon","42 Jason","42 Errol","41 Garry","41 Bernard","41 Barry","39 Stephen"
</syntaxhighlight>
<syntaxhighlight lang="powershell">
$scores | Get-Ranking -Standard
</syntaxhighlight>
{{Out}}
<pre>
Line 3,827 ⟶ 4,576:
Stephen 39 7
</pre>
<syntaxhighlight lang="powershell">
$scores | Get-Ranking -Modified
</syntaxhighlight>
{{Out}}
<pre>
Line 3,842 ⟶ 4,591:
Stephen 39 7
</pre>
<syntaxhighlight lang="powershell">
$scores | Get-Ranking -Dense
</syntaxhighlight>
{{Out}}
<pre>
Line 3,857 ⟶ 4,606:
Stephen 39 4
</pre>
<syntaxhighlight lang="powershell">
$scores | Get-Ranking -Ordinal
</syntaxhighlight>
{{Out}}
<pre>
Line 3,872 ⟶ 4,621:
Stephen 39 7
</pre>
<syntaxhighlight lang="powershell">
$scores | Get-Ranking -Fractional
</syntaxhighlight>
{{Out}}
<pre>
Line 3,889 ⟶ 4,638:
=={{header|Python}}==
<
"""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))</
{{out}}
Line 4,020 ⟶ 4,769:
=={{header|Racket}}==
<
;; Tim-brown 2014-09-11
Line 4,096 ⟶ 4,845:
(caddr r)
(cdddr r)))
(newline))</
{{out}}
Line 4,146 ⟶ 4,895:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
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;</
{{out}}
<pre>Standard:
Line 4,226 ⟶ 4,975:
=={{header|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'</
{{out}}
<pre>44 Solomon 1 1 1 1 1
Line 4,286 ⟶ 5,035:
=={{header|Ruby}}==
<
42 Jason
42 Errol
Line 4,306 ⟶ 5,055:
end
s_rnk += names.size
end</
{{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.
<
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")
}</
{{out}}
<pre>Standard:
Line 4,401 ⟶ 5,150:
=={{header|Sidef}}==
{{trans|Raku}}
<
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))</
{{out}}
<pre>
Line 4,500 ⟶ 5,249:
=={{header|Tcl}}==
<
# 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"
}
}</
{{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}}==
<
#DEFINE CTAB CHR(9)
#DEFINE CRLF CHR(13) + CHR(10)
Line 4,709 ⟶ 5,521:
ENDFOR
ENDPROC
</syntaxhighlight>
{{out}}
<pre>
Line 4,726 ⟶ 5,538:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
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)</
{{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}}==
<
sink:=List();
scores.reduce('wrap(ps,sn,buf){
Line 4,891 ⟶ 5,763:
print(group,"%5.2f".fmt(r));
}
}</
<
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);</
{{out}}
<pre>
|