Avatar of w1zeman1p

w1zeman1p's solution

to Tournament in the Ruby Track

Published at Mar 04 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Tally the results of a small football competition.

Based on an input file containing which team played against which and what the outcome was, create a file with a table like this:

Team                           | MP |  W |  D |  L |  P
Devastating Donkeys            |  3 |  2 |  1 |  0 |  7
Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6
Blithering Badgers             |  3 |  1 |  0 |  2 |  3
Courageous Californians        |  3 |  0 |  1 |  2 |  1

What do those abbreviations mean?

  • MP: Matches Played
  • W: Matches Won
  • D: Matches Drawn (Tied)
  • L: Matches Lost
  • P: Points

A win earns a team 3 points. A draw earns 1. A loss earns 0.

The outcome should be ordered by points, descending. In case of a tie, teams are ordered alphabetically.

Input

Your tallying program will receive input that looks like:

Allegoric Alaskans;Blithering Badgers;win
Devastating Donkeys;Courageous Californians;draw
Devastating Donkeys;Allegoric Alaskans;win
Courageous Californians;Blithering Badgers;loss
Blithering Badgers;Devastating Donkeys;loss
Allegoric Alaskans;Courageous Californians;win

The result of the match refers to the first team listed. So this line

Allegoric Alaskans;Blithering Badgers;win

Means that the Allegoric Alaskans beat the Blithering Badgers.

This line:

Courageous Californians;Blithering Badgers;loss

Means that the Blithering Badgers beat the Courageous Californians.

And this line:

Devastating Donkeys;Courageous Californians;draw

Means that the Devastating Donkeys and Courageous Californians tied.


For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby tournament_test.rb

To include color from the command line:

ruby -r minitest/pride tournament_test.rb

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

tournament_test.rb

require 'minitest/autorun'
require_relative 'tournament'

# Common test data version: 1.4.0 ee01fe0
class TournamentTest < Minitest::Test
  def test_just_the_header_if_no_input
    # skip
    input = <<-INPUT.gsub(/^ */, '')

    INPUT

    expected = <<-TALLY.gsub(/^ */, '')
    Team                           | MP |  W |  D |  L |  P
    TALLY

    assert_equal expected, Tournament.tally(input)
  end

  def test_a_win_is_three_points_a_loss_is_zero_points
    skip
    input = <<-INPUT.gsub(/^ */, '')
    Allegoric Alaskans;Blithering Badgers;win
    INPUT

    expected = <<-TALLY.gsub(/^ */, '')
    Team                           | MP |  W |  D |  L |  P
    Allegoric Alaskans             |  1 |  1 |  0 |  0 |  3
    Blithering Badgers             |  1 |  0 |  0 |  1 |  0
    TALLY

    assert_equal expected, Tournament.tally(input)
  end

  def test_a_win_can_also_be_expressed_as_a_loss
    skip
    input = <<-INPUT.gsub(/^ */, '')
    Blithering Badgers;Allegoric Alaskans;loss
    INPUT

    expected = <<-TALLY.gsub(/^ */, '')
    Team                           | MP |  W |  D |  L |  P
    Allegoric Alaskans             |  1 |  1 |  0 |  0 |  3
    Blithering Badgers             |  1 |  0 |  0 |  1 |  0
    TALLY

    assert_equal expected, Tournament.tally(input)
  end

  def test_a_different_team_can_win
    skip
    input = <<-INPUT.gsub(/^ */, '')
    Blithering Badgers;Allegoric Alaskans;win
    INPUT

    expected = <<-TALLY.gsub(/^ */, '')
    Team                           | MP |  W |  D |  L |  P
    Blithering Badgers             |  1 |  1 |  0 |  0 |  3
    Allegoric Alaskans             |  1 |  0 |  0 |  1 |  0
    TALLY

    assert_equal expected, Tournament.tally(input)
  end

  def test_a_draw_is_one_point_each
    skip
    input = <<-INPUT.gsub(/^ */, '')
    Allegoric Alaskans;Blithering Badgers;draw
    INPUT

    expected = <<-TALLY.gsub(/^ */, '')
    Team                           | MP |  W |  D |  L |  P
    Allegoric Alaskans             |  1 |  0 |  1 |  0 |  1
    Blithering Badgers             |  1 |  0 |  1 |  0 |  1
    TALLY

    assert_equal expected, Tournament.tally(input)
  end

  def test_there_can_be_more_than_one_match
    skip
    input = <<-INPUT.gsub(/^ */, '')
    Allegoric Alaskans;Blithering Badgers;win
    Allegoric Alaskans;Blithering Badgers;win
    INPUT

    expected = <<-TALLY.gsub(/^ */, '')
    Team                           | MP |  W |  D |  L |  P
    Allegoric Alaskans             |  2 |  2 |  0 |  0 |  6
    Blithering Badgers             |  2 |  0 |  0 |  2 |  0
    TALLY

    assert_equal expected, Tournament.tally(input)
  end

  def test_there_can_be_more_than_one_winner
    skip
    input = <<-INPUT.gsub(/^ */, '')
    Allegoric Alaskans;Blithering Badgers;loss
    Allegoric Alaskans;Blithering Badgers;win
    INPUT

    expected = <<-TALLY.gsub(/^ */, '')
    Team                           | MP |  W |  D |  L |  P
    Allegoric Alaskans             |  2 |  1 |  0 |  1 |  3
    Blithering Badgers             |  2 |  1 |  0 |  1 |  3
    TALLY

    assert_equal expected, Tournament.tally(input)
  end

  def test_there_can_be_more_than_two_teams
    skip
    input = <<-INPUT.gsub(/^ */, '')
    Allegoric Alaskans;Blithering Badgers;win
    Blithering Badgers;Courageous Californians;win
    Courageous Californians;Allegoric Alaskans;loss
    INPUT

    expected = <<-TALLY.gsub(/^ */, '')
    Team                           | MP |  W |  D |  L |  P
    Allegoric Alaskans             |  2 |  2 |  0 |  0 |  6
    Blithering Badgers             |  2 |  1 |  0 |  1 |  3
    Courageous Californians        |  2 |  0 |  0 |  2 |  0
    TALLY

    assert_equal expected, Tournament.tally(input)
  end

  def test_typical_input
    skip
    input = <<-INPUT.gsub(/^ */, '')
    Allegoric Alaskans;Blithering Badgers;win
    Devastating Donkeys;Courageous Californians;draw
    Devastating Donkeys;Allegoric Alaskans;win
    Courageous Californians;Blithering Badgers;loss
    Blithering Badgers;Devastating Donkeys;loss
    Allegoric Alaskans;Courageous Californians;win
    INPUT

    expected = <<-TALLY.gsub(/^ */, '')
    Team                           | MP |  W |  D |  L |  P
    Devastating Donkeys            |  3 |  2 |  1 |  0 |  7
    Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6
    Blithering Badgers             |  3 |  1 |  0 |  2 |  3
    Courageous Californians        |  3 |  0 |  1 |  2 |  1
    TALLY

    assert_equal expected, Tournament.tally(input)
  end

  def test_incomplete_competition_not_all_pairs_have_played
    skip
    input = <<-INPUT.gsub(/^ */, '')
    Allegoric Alaskans;Blithering Badgers;loss
    Devastating Donkeys;Allegoric Alaskans;loss
    Courageous Californians;Blithering Badgers;draw
    Allegoric Alaskans;Courageous Californians;win
    INPUT

    expected = <<-TALLY.gsub(/^ */, '')
    Team                           | MP |  W |  D |  L |  P
    Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6
    Blithering Badgers             |  2 |  1 |  1 |  0 |  4
    Courageous Californians        |  2 |  0 |  1 |  1 |  1
    Devastating Donkeys            |  1 |  0 |  0 |  1 |  0
    TALLY

    assert_equal expected, Tournament.tally(input)
  end

  def test_ties_broken_alphabetically
    skip
    input = <<-INPUT.gsub(/^ */, '')
    Courageous Californians;Devastating Donkeys;win
    Allegoric Alaskans;Blithering Badgers;win
    Devastating Donkeys;Allegoric Alaskans;loss
    Courageous Californians;Blithering Badgers;win
    Blithering Badgers;Devastating Donkeys;draw
    Allegoric Alaskans;Courageous Californians;draw
    INPUT

    expected = <<-TALLY.gsub(/^ */, '')
    Team                           | MP |  W |  D |  L |  P
    Allegoric Alaskans             |  3 |  2 |  1 |  0 |  7
    Courageous Californians        |  3 |  2 |  1 |  0 |  7
    Blithering Badgers             |  3 |  0 |  1 |  2 |  1
    Devastating Donkeys            |  3 |  0 |  1 |  2 |  1
    TALLY

    assert_equal expected, Tournament.tally(input)
  end
end
class Team
  attr_reader :points, :name, :wins, :losses, :draws

  def initialize(name)
    @name = name
    @wins, @losses, @draws = 0, 0, 0
  end

  def won!
    @wins += 1
  end

  def drew!
    @draws += 1
  end

  def lost!
    @losses += 1
  end

  def points
    (wins * 3) + draws
  end

  def played
    wins + losses + draws
  end
end

class Match
  def initialize(team1, team2, result)
    @team1 = team1
    @team2 = team2
    @result = result
  end

  def score!
    if draw?
      team1.drew!
      team2.drew!
    elsif team1_win?
      team1.won!
      team2.lost!
    else
      team1.lost!
      team2.won!
    end
  end

  private

  attr_reader :team1, :team2, :result

  def draw?
    result == "draw"
  end

  def team1_win?
    result == "win"
  end
end

class TournamentTextFormatter
  ROW_FORMAT = "%-30s | %2s | %2s | %2s | %2s | %2s\n".freeze
  HEADER = %w(Team MP W D L P).freeze

  def self.format(teams)
    rows = [HEADER].concat(teams.map {|t| outcome(t)})
    rows.inject("") do |result, outcome|
      "#{result}#{ROW_FORMAT % outcome}"
    end
  end

  def self.outcome(team)
    [
      team.name,
      team.played,
      team.wins,
      team.draws,
      team.losses,
      team.points
    ]
  end
end

class Tournament
  attr_reader :teams, :matches

  def self.tally(raw_matches, formatter=TournamentTextFormatter)
    tournament = Tournament.new

    raw_matches.each_line do |match|
      break if match.chomp!.empty?
      team1, team2, result = match.split(";")
      team1 = tournament.teams[team1]
      team2 = tournament.teams[team2]
      Match.new(team1, team2, result).score!
    end

    formatter.format(tournament.tally)
  end

  def initialize
    @teams = Hash.new{|h, k| h[k] = Team.new(k)}
  end

  def tally
    @teams
      .values
      .sort_by do |team|
        [team.points * -1, team.name]
      end
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?