# 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?

(Frank J. Cameron) #1

Gerald Bauer wrote:

Challenge #9 - Tally Up / Calculate the Standings Table for the
English Premier League 2018/19 Season

\$ ruby -v lib/009.rb
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
Run options: --seed 17997
# Running:
..
Finished in 0.009669s, 206.8368 runs/s, 206.8368 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

\$ cat lib/009.rb
require_relative '../009/test.rb'
class RubyQuizTest

···

#
# [ 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
Dir.chdir("#{__dir__}/../009")
RubyQuizTest.new('fjc')

(Gerald Bauer) #2

Hello,

Wow. Great tallyup version.You rock (ruby). As always thanks for
posting / sharing.

For reference here's my test answer using the sportdb text library / gem.

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

# 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

Merry Christmas. Happy New Year. Prosit 2019! See you in 2019. Cheers. Prost.