Ranking methods: Difference between revisions

→‎Tcl: Added implementation
m (→‎{{header|Ruby}}: no longer incorrect by my reading of task)
(→‎Tcl: Added implementation)
Line 912:
4 6 3 6 5 41 Barry
7 7 4 7 7 39 Stephen
</pre>
 
=={{header|Tcl}}==
<lang tcl>proc rank {rankingMethod sortedList} {
# Extract the groups in the data (this is pointless for ordinal...)
set s [set group [set groups {}]]
foreach {score who} $sortedList {
if {$score != $s} {
lappend groups [llength $group]
set s $score
set group {}
}
lappend group $who
}
lappend groups [llength $group]
# Construct the rankings; note that we have a zero-sized leading group
set n 1; set m 0
foreach g $groups {
switch $rankingMethod {
standard {
lappend result {*}[lrepeat $g $n]
incr n $g
}
modified {
lappend result {*}[lrepeat $g [incr m $g]]
}
dense {
lappend result {*}[lrepeat $g $m]
incr m
}
ordinal {
for {set i 0} {$i < $g} {incr i} {
lappend result [incr m]
}
}
fractional {
set val [expr {($n + [incr n $g] - 1) / 2.0}]
lappend result {*}[lrepeat $g [format %g $val]]
}
}
}
return $result
}
 
set data {
44 Solomon
42 Jason
42 Errol
41 Garry
41 Bernard
41 Barry
39 Stephen
}
foreach method {standard modified dense ordinal fractional} {
puts "Using method '$method'...\n Rank\tScore\tWho"
foreach rank [rank $method $data] {score who} $data {
puts " $rank\t$score\t$who"
}
}</lang>
{{out}}
<pre>
Using method 'standard'...
Rank Score Who
1 44 Solomon
2 42 Jason
2 42 Errol
4 41 Garry
4 41 Bernard
4 41 Barry
7 39 Stephen
Using method 'modified'...
Rank Score Who
1 44 Solomon
3 42 Jason
3 42 Errol
6 41 Garry
6 41 Bernard
6 41 Barry
7 39 Stephen
Using method 'dense'...
Rank Score Who
1 44 Solomon
2 42 Jason
2 42 Errol
3 41 Garry
3 41 Bernard
3 41 Barry
4 39 Stephen
Using method 'ordinal'...
Rank Score Who
1 44 Solomon
2 42 Jason
3 42 Errol
4 41 Garry
5 41 Bernard
6 41 Barry
7 39 Stephen
Using method 'fractional'...
Rank Score Who
1 44 Solomon
2.5 42 Jason
2.5 42 Errol
5 41 Garry
5 41 Bernard
5 41 Barry
7 39 Stephen
</pre>
 
Anonymous user