Jump to content

Ranking methods: Difference between revisions

m
→‎{{header|AppleScript}}: New sort handler URL, tidy-up.
m (syntax highlighting fixup automation)
m (→‎{{header|AppleScript}}: New sort handler URL, tidy-up.)
Line 184:
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use AppleScript version "2.43.1" -- Mac OS 10.119 (YosemiteMavericks) or later (for these 'use' commands).
use sorter : script ¬
use sorter : script "Custom Iterative Ternary Merge Sort" -- <https://www.macscripter.net/viewtopic.php?pid=194430#p194430t/timsort-and-nigsort/71383/3>
 
(* The five ranking methods are implemented here as script objects sharing inherited code. rather than as simple*)
<syntaxhighlight 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 '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 207 ⟶ 203:
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 localVariabler 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 238 ⟶ 228:
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 253 ⟶ 243:
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 259 ⟶ 249:
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 267 ⟶ 257:
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 275 ⟶ 265:
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 295 ⟶ 284:
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 joinStringsjoin(output, linefeed & linefeed)</syntaxhighlight>
 
{{output}}
557

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.