Ranking methods: Difference between revisions
Content added Content deleted
(Added AppleScript.) |
Not a robot (talk | contribs) (Add Cowgol) |
||
Line 1,057: | Line 1,057: | ||
5.0 41 Gary |
5.0 41 Gary |
||
7.0 39 Stephen</pre> |
7.0 39 Stephen</pre> |
||
=={{header|Cowgol}}== |
|||
<lang cowgol>include "cowgol.coh"; |
|||
# List of competitors |
|||
record Competitor is |
|||
score: uint8; |
|||
name: [uint8]; |
|||
end record; |
|||
var cs: Competitor[] := { |
|||
{44, "Solomon"}, |
|||
{42, "Jason"}, |
|||
{42, "Errol"}, |
|||
{41, "Garry"}, |
|||
{41, "Bernard"}, |
|||
{41, "Barry"}, |
|||
{39, "Stephen"} |
|||
}; |
|||
# Rank competitors given ranking method |
|||
interface Ranking(c: [Competitor], |
|||
n: intptr, |
|||
len: intptr, |
|||
last: uint16): (rank: uint16); |
|||
sub Rank(cs: [Competitor], num: intptr, r: Ranking) is |
|||
var last: uint16 := 0; |
|||
var idx: intptr := 0; |
|||
while idx < num loop |
|||
last := r(cs, idx, num, last); |
|||
if last < 100 then |
|||
print_i16(last); |
|||
else |
|||
# print fixed-point rank nicely |
|||
print_i16(last / 100); |
|||
print_char('.'); |
|||
print_i16((last % 100) / 10); |
|||
print_i16(last % 10); |
|||
end if; |
|||
print(". "); |
|||
print_i8(cs.score); |
|||
print(", "); |
|||
print(cs.name); |
|||
print_nl(); |
|||
idx := idx + 1; |
|||
cs := @next cs; |
|||
end loop; |
|||
end sub; |
|||
# Standard ranking |
|||
var stdcount: uint16 := 0; |
|||
sub Standard implements Ranking is |
|||
if n==0 then stdcount := 0; end if; |
|||
stdcount := stdcount + 1; |
|||
if n>0 and c.score == [@prev c].score then |
|||
rank := last; |
|||
else |
|||
rank := stdcount; |
|||
end if; |
|||
end sub; |
|||
# Modified ranking |
|||
sub Modified implements Ranking is |
|||
rank := last; |
|||
if n == 0 or c.score != [@prev c].score then |
|||
while n < len loop |
|||
rank := rank + 1; |
|||
c := @next c; |
|||
if c.score != [@prev c].score then |
|||
break; |
|||
end if; |
|||
n := n + 1; |
|||
end loop; |
|||
end if; |
|||
end sub; |
|||
# Dense ranking |
|||
sub Dense implements Ranking is |
|||
if n>0 and c.score == [@prev c].score then |
|||
rank := last; |
|||
else |
|||
rank := last + 1; |
|||
end if; |
|||
end sub; |
|||
# Ordinal ranking |
|||
sub Ordinal implements Ranking is |
|||
rank := last + 1; |
|||
end sub; |
|||
# Fractional ranking (with fixed point arithmetic) |
|||
sub Fractional implements Ranking is |
|||
rank := last; |
|||
if n==0 or c.score != [@prev c].score then |
|||
var sum: uint16 := 0; |
|||
var ct: uint16 := 0; |
|||
while n < len loop |
|||
sum := sum + (n as uint16 + 1); |
|||
ct := ct + 1; |
|||
c := @next c; |
|||
if c.score != [@prev c].score then |
|||
break; |
|||
end if; |
|||
n := n + 1; |
|||
end loop; |
|||
rank := (sum * 100) / (ct as uint16); |
|||
end if; |
|||
end sub; |
|||
record Method is |
|||
name: [uint8]; |
|||
method: Ranking; |
|||
end record; |
|||
var methods: Method[] := { |
|||
{"Standard", Standard}, |
|||
{"Modified", Modified}, |
|||
{"Dense", Dense}, |
|||
{"Ordinal", Ordinal}, |
|||
{"Fractional", Fractional} |
|||
}; |
|||
var n: @indexof methods := 0; |
|||
while n < @sizeof methods loop |
|||
print("--- "); |
|||
print(methods[n].name); |
|||
print(" ---\n"); |
|||
Rank(&cs[0], @sizeof cs, methods[n].method); |
|||
print_nl(); |
|||
n := n + 1; |
|||
end loop;</lang> |
|||
{{out}} |
|||
<pre>--- Standard --- |
|||
1. 44, Solomon |
|||
2. 42, Jason |
|||
2. 42, Errol |
|||
4. 41, Garry |
|||
4. 41, Bernard |
|||
4. 41, Barry |
|||
7. 39, Stephen |
|||
--- Modified --- |
|||
1. 44, Solomon |
|||
3. 42, Jason |
|||
3. 42, Errol |
|||
6. 41, Garry |
|||
6. 41, Bernard |
|||
6. 41, Barry |
|||
7. 39, Stephen |
|||
--- Dense --- |
|||
1. 44, Solomon |
|||
2. 42, Jason |
|||
2. 42, Errol |
|||
3. 41, Garry |
|||
3. 41, Bernard |
|||
3. 41, Barry |
|||
4. 39, Stephen |
|||
--- Ordinal --- |
|||
1. 44, Solomon |
|||
2. 42, Jason |
|||
3. 42, Errol |
|||
4. 41, Garry |
|||
5. 41, Bernard |
|||
6. 41, Barry |
|||
7. 39, Stephen |
|||
--- Fractional --- |
|||
1.00. 44, Solomon |
|||
2.50. 42, Jason |
|||
2.50. 42, Errol |
|||
5.00. 41, Garry |
|||
5.00. 41, Bernard |
|||
5.00. 41, Barry |
|||
7.00. 39, Stephen</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |