Ranking methods: Difference between revisions

Added AppleScript.
m (→‎{{header|Sidef}}: Fix link: Perl 6 --> Raku)
(Added AppleScript.)
Line 27:
Show here, on this page, the ranking of the test scores under each of the numbered ranking methods.
<br><br>
 
=={{header|AppleScript}}==
 
<lang applescript>(*
The five ranking methods are implemented here as script objects sharing inherited code rather than as simple
handlers, although there appears to be little advantage either way. This way needs fewer lines of code
but more lines of explanatory comments!
*)
use AppleScript version "2.4" -- Mac OS 10.11 (Yosemite) or later (for these 'use' commands).
use sorter : script "Custom Iterative Ternary Merge Sort" -- <https://macscripter.net/viewtopic.php?pid=194430#p194430>
 
script standardRanking
-- Properties and handlers inherited or overridden by the other script objects.
-- The 'references' are to values that won't be available until 'results' is set to a list.
-- 'me' and 'my' refer to the script object using the code at the time.
property results : missing value
property startIndex : 1
property endIndex : a reference to length of (a reference to results)
property step : 1
property currentRank : missing value
property currentScore : missing value
-- Main handler.
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 score of item (my startIndex) of my results
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
-- 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 localVariable
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 ranking handler. Inherited by the modifiedRanking script, but overridden by the others.
on rankResult(i)
set thisResult to item i of my results
set thisScore to thisResult's score
if (thisScore is not currentScore) then
set my currentRank to i
set my currentScore to thisScore
end if
set 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 length of (a reference to results)
property endIndex : 1
property step : -1
end script
 
script denseRanking
property parent : standardRanking
on rankResult(i)
set thisResult to item i of my results
set thisScore to thisResult's score
if (thisScore is not my currentScore) then
set my currentRank to (my currentRank) + 1
set my currentScore to thisScore
end if
set item i of my results to thisResult & {rank:my currentRank}
end rankResult
end script
 
script ordinalRanking
property parent : standardRanking
on rankResult(i)
set item i of my results to (item i of my results) & {rank:i}
end rankResult
end script
 
script fractionalRanking
property parent : standardRanking
on rankResult(i)
set thisResult to 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 rank of item j of my results to average
end repeat
set my currentRank to i
set my currentScore to thisScore
end if
set item i of my results to thisResult & {rank:i as real}
end rankResult
end script
 
-- Task code:
on formatRankings(type, theResults)
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 joinStrings(rankings, linefeed)
end formatRankings
 
on joinStrings(listOfText, delimiter)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set output to listOfText as text
set AppleScript's text item delimiters to astid
return output
end joinStrings
 
local theScores, output
set theScores to {{score:44, competitor:"Solomon"}, {score:42, competitor:"Jason"}, {score:42, competitor:"Errol"}, ¬
{score:41, competitor:"Garry"}, {score:41, competitor:"Bernard"}, {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>
 
{{output}}
<lang applescript>"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)"</lang>
 
=={{header|AutoHotkey}}==
557

edits