Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,62 @@
var scores = [
Pair(Solomon => 44),
Pair(Jason => 42),
Pair(Errol => 42),
Pair(Garry => 41),
Pair(Bernard => 41),
Pair(Barry => 41),
Pair(Stephen => 39),
]
func tiers(s) {
s.group_by { .value }.kv.sort.flip.map { .value.map{.key} }
}
func standard(s) {
var rank = 1
gather {
for players in tiers(s) {
take(Pair(rank, players))
rank += players.len
}
}
}
func modified(s) {
var rank = 0
gather {
for players in tiers(s) {
rank += players.len
take(Pair(rank, players))
}
}
}
func dense(s) {
tiers(s).map_kv { |k,v| Pair(k+1, v) }
}
func ordinal(s) {
s.map_kv { |k,v| Pair(k+1, v.key) }
}
func fractional(s) {
var rank = 1
gather {
for players in tiers(s) {
var beg = rank
var end = (rank += players.len)
take(Pair(sum(beg ..^ end) / players.len, players))
}
}
}
func display(r) {
say r.map {|a| '%3s : %s' % a... }.join("\n")
}
say "Standard:"; display( standard(scores))
say "\nModified:"; display( modified(scores))
say "\nDense:"; display( dense(scores))
say "\nOrdinal:"; display( ordinal(scores))
say "\nFractional:"; display(fractional(scores))

View file

@ -0,0 +1,102 @@
#DEFINE CTAB CHR(9)
#DEFINE CRLF CHR(13) + CHR(10)
LOCAL lcTxt As String, i As Integer
CLOSE DATABASES ALL
SET SAFETY OFF
CLEAR
CREATE CURSOR scores (score I, name V(8), rownum I AUTOINC)
INDEX ON score TAG score COLLATE "Machine"
SET ORDER TO 0
INSERT INTO scores (score, name) VALUES (44, "Solomon")
INSERT INTO scores (score, name) VALUES (42, "Jason")
INSERT INTO scores (score, name) VALUES (42, "Errol")
INSERT INTO scores (score, name) VALUES (41, "Garry")
INSERT INTO scores (score, name) VALUES (41, "Bernard")
INSERT INTO scores (score, name) VALUES (41, "Barry")
INSERT INTO scores (score, name) VALUES (39, "Stephen")
CREATE CURSOR ranks (sc_rank I, mod_rank I, dense I, ordinal I, fractional B(1), score I, name V(8))
INDEX ON score TAG score COLLATE "Machine"
SET ORDER TO 0
APPEND FROM DBF("scores") FIELDS score, name
Std_Comp()
Modified()
Dense()
Ordinal()
Fractional()
COPY TO ranks.txt TYPE DELIMITED WITH TAB
lcTxt = ""
FOR i = 1 TO FCOUNT()
lcTxt = lcTxt + FIELD(i) + CTAB
ENDFOR
lcTxt = LEFT(lcTxt, LEN(lcTxt) - 1) + CRLF + FILETOSTR("ranks.txt")
STRTOFILE(lcTxt, "ranks.txt")
MODIFY FILE ranks.txt
SET SAFETY ON
FUNCTION ScoreGroup(aa)
LOCAL n As Integer
SELECT score, COUNT(*) As num FROM scores ;
GROUP BY score ORDER BY score DESC INTO ARRAY aa
n = _TALLY
RETURN n
ENDFUNC
PROCEDURE Std_Comp
LOCAL n, i, nn
LOCAL ARRAY a[1]
SELECT ranks
BLANK FIELDS sc_rank ALL
nn = ScoreGroup(@a)
n = 1
FOR i = 1 TO nn
REPLACE sc_rank WITH n FOR score = a[i,1]
n = n + a[i,2]
ENDFOR
ENDPROC
PROCEDURE Modified
LOCAL n, i, nn
LOCAL ARRAY a[1]
SELECT ranks
BLANK FIELDS mod_rank ALL
nn = ScoreGroup(@a)
n = 0
FOR i = 1 TO nn
n = n + a[i,2]
REPLACE mod_rank WITH n FOR score = a[i,1]
ENDFOR
ENDPROC
PROCEDURE Dense
LOCAL n, i, nn
LOCAL ARRAY a[1]
SELECT ranks
BLANK FIELDS dense ALL
nn = ScoreGroup(@a)
SELECT ranks
n = 0
FOR i = 1 TO nn
n = n + a[i,2]
REPLACE dense WITH i FOR score = a[i,1]
ENDFOR
ENDPROC
PROCEDURE Ordinal
SELECT ranks
BLANK FIELDS ordinal ALL
REPLACE ordinal WITH RECNO() ALL
ENDPROC
PROCEDURE Fractional
LOCAL i As Integer, nn As Integer
LOCAL ARRAY a[1]
SELECT ranks
BLANK FIELDS fractional ALL
SELECT CAST(AVG(rownum) As B(1)), score FROM scores ;
GROUP BY score ORDER BY score DESC INTO ARRAY a
nn = _TALLY
FOR i = 1 TO nn
REPLACE fractional WITH a[i,1] FOR score = a[i,2]
ENDFOR
ENDPROC

View file

@ -0,0 +1,57 @@
# Ties share what would have been their first ordinal number
def standard_ranking:
. as $raw
| ([range(1;length;2) | $raw[.]]) as $scores
| reduce range(1; $scores|length) as $i
([1]; if $scores[$i - 1] == $scores[$i] then . + [.[-1]]
else . + [$i + 1]
end) ;
def modified_ranking:
# The helper function resolves [ranks, tentative]
# by appending the ranks of the ties to "ranks"
def resolve:
(.[1] | length) as $length
| if $length == 0 then .[0]
else .[1][-1] as $max
| .[0] + ( .[1] | map( $max) )
end ;
. as $raw
| ([range(1;length;2) | $raw[.]]) as $scores
| reduce range(1; $scores|length) as $i
# state: [ranks, tentative]
([ [], [1] ];
if $scores[$i - 1] == $scores[$i] then [.[0], .[1] + [ $i + 1 ]]
else [ resolve, [ $i + 1 ] ]
end )
| resolve ;
def dense_ranking: # next available
. as $raw
| ([range(1;length;2) | $raw[.]]) as $scores
| reduce range(1; $scores|length) as $i
([1]; if $scores[$i - 1] == $scores[$i] then . + [.[-1]]
else . + [ .[-1] + 1]
end );
def ordinal_ranking: # unfair to some!
[ range(1; 1 + length/2) ] ;
def fractional_ranking:
# The helper function resolves [ranks, tentative]
# by appending the averages of the tentative ranks to "ranks"
def resolve:
(.[1] | length) as $length
| if $length == 0 then .[0]
else (.[1] | add / $length) as $avg
| .[0] + ( .[1] | map( $avg) )
end ;
. as $raw
| ([range(1;length;2) | $raw[.]]) as $scores
| reduce range(1; $scores|length) as $i
# state: [ranks, tentative]
([ [], [1] ];
if $scores[$i - 1] == $scores[$i] then [.[0], .[1] + [ $i + 1 ]]
else [ resolve, [ $i + 1 ] ]
end )
| resolve ;

View file

@ -0,0 +1,19 @@
def raw:
[
"Solomon", 44,
"Jason" , 42,
"Errol" , 42,
"Garry" , 41,
"Bernard", 41,
"Barry" , 41,
"Stephen", 39
] ;
def task:
"standard: \( raw | standard_ranking)",
"modified: \( raw | modified_ranking)",
"dense: \( raw | dense_ranking)",
"ordinal: \( raw | ordinal_ranking)",
"fractional: \( raw | fractional_ranking)" ;
task