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