Ruby, concise as usual:
class LeagueTable def initialize @results = Hash.new(Hash.new(0)) end def push str str.match /^(.+) (\d+) - (\d+) (.+)$/ h_name, a_name = $1, $4 h_goal, a_goal = $2.to_i, $3.to_i result = h_goal <=> a_goal @results[h_name] = Hash.new(0) unless @results.has_key? h_name @results[a_name] = Hash.new(0) unless @results.has_key? a_name @results[h_name].tap do |stats| stats[:goals] += h_goal stats[:conceded] += a_goal stats[:wins] += (result + 1) / 2 stats[:losses] += (-result + 1) / 2 stats[:draws] += -(result.abs) + 1 end @results[a_name].tap do |stats| stats[:goals] += a_goal stats[:conceded] += h_goal stats[:wins] += (-result + 1) / 2 stats[:losses] += (result + 1) / 2 stats[:draws] += -(result.abs) + 1 end end def goals_for team @results[team][:goals] end def goals_against team @results[team][:conceded] end def goal_difference team @results[team][:goals] - @results[team][:conceded] end def get_wins team @results[team][:wins] end def get_draws team @results[team][:draws] end def get_losses team @results[team][:losses] end def get_points team 3*@results[team][:wins] + @results[team][:draws] end end
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Ruby, concise as usual: