-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.rb
80 lines (72 loc) · 2 KB
/
solution.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
####
# Entry by Frank J. Cameron
# see https://rubytalk.org/t/re-ruby-quiz-challenge-9-tally-up-calculate-the-standings-table-for-the-english-premier-league-2018-19-season-and-the-winner-is-liverpool-manchester-city/74884
module Frank
# [ Team 1, Raw Score (Score 1 - Score 2), Team 2 ]
#
# Team Name:
# [ 0: Played | 1: Won | 2: Drawn | 3: Lost |
# | 4: Goals for | 5: Goals against |
# | 6: Points (+3/+1 for win/tie) ]
#
def tallyup(matches)
Hash.new{|h,k| h[k] = Array.new(7){0}}.tap{|h|
matches.drop(1).each do |t1, rs, t2|
s1, s2 = rs.split('-').map(&:to_i)
h[t1][0] += 1; h[t2][0] += 1
h[t1][4] += s1; h[t2][4] += s2
h[t1][5] += s2; h[t2][5] += s1
case s1 <=> s2
when 1
h[t1][1] += 1; h[t2][3] += 1; h[t1][6] += 3
when 0
h[t1][2] += 1; h[t1][6] += 1
h[t2][2] += 1; h[t2][6] += 1
else
h[t1][3] += 1; h[t2][1] += 1; h[t2][6] += 3
end
end
}
.sort_by{|_,v| [-v[6], v[5]-v[4], -v[4]]}
.map {|k,v| [k]+v}
end
end # module Frank
#############
# Test entry
# by Gerald Bauer
module TestAnswer
def tallyup( matches )
require 'sportdb/text'
# step 1: convert match array to match "struct / named tuple"
matches = matches.map do |match|
team1 = match[0]
score1, score2 = match[1].split('-')
team2 = match[2]
SportDb::Struct::Match.create(
team1: team1,
score1: score1,
score2: score2,
team2: team2
)
end
# step 2: tally up standings
standings = SportDb::Struct::Standings.new
standings.update( matches )
## note: for the black box "magic" see http://github.com/yorobot/football.csv/blob/master/sportdb-text/lib/sportdb/text/structs/standings.rb#L175
# step 3: convert to array of arrays
recs = []
standings.to_a.each do |l|
recs << [
l.team,
l.played,
l.won,
l.drawn,
l.lost,
l.goals_for,
l.goals_against,
l.pts
]
end
recs
end
end