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" <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <[email protected]>
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')



Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.