Sha256: 11c775b3b90bbecabdbee96b330737bd6e3eac0e760cbb0a82286c73d26c11f7

Contents?: true

Size: 1.26 KB

Versions: 230

Compression:

Stored size: 1.26 KB

Contents

module BookKeeping
  VERSION = 3
end

class Tournament
  def self.tally(input)
    teams = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = 0 } }

    input.split("\n").each do |line|
      matchdata = line.match(/^(.*);(.*);(.*)$/)
      next unless matchdata
      team_one, team_two, result = matchdata.captures

      case result
      when "win"
        teams[team_one][:won] += 1
        teams[team_two][:loss] += 1
      when "loss"
        teams[team_one][:loss] += 1
        teams[team_two][:won] += 1
      when "draw"
        teams[team_one][:draw] += 1
        teams[team_two][:draw] += 1
      end
    end

    teams.keys.each do |team_name|
      team = teams[team_name]
      team[:matches] = team[:won] + team[:loss] + team[:draw]
      team[:points] = team[:won] * 3 + team[:draw]
    end

    team_order =
      teams
      .to_a
      .sort_by { |team| [-team[1][:points], team[0]] }
      .map(&:first)

    result = ['Team                           | MP |  W |  D |  L |  P']

    team_order.each do |team_name|
      stats = teams[team_name]

      result << [
        team_name.ljust(30),
        stats[:matches],
        stats[:won],
        stats[:draw],
        stats[:loss],
        stats[:points]
      ].join(' |  ')
    end

    result.join("\n") + "\n"
  end
end

Version data entries

230 entries across 230 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.118 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.117 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.116 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.115 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.114 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.113 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.111 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.110 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.109 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.108 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.107 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.106 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.105 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.104 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.103 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.102 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.101 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.100 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb
trackler-2.2.1.99 tracks/ruby/exercises/tournament/.meta/solutions/tournament.rb