# artemkorsakov's solution

## to Tournament in the C# Track

Published at May 25 2019 · 0 comments
Instructions
Test suite
Solution

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

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

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

``````Allegoric Alaskans;Blithering Badgers;win
``````

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.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit Tournament.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

### TournamentTest.cs

``````// This file was auto-generated based on version 1.4.0 of the canonical data.

using System;
using System.IO;
using System.Text;
using Xunit;

public class TournamentTest
{
[Fact]
{
var rows = "";
var expected = "Team                           | MP |  W |  D |  L |  P";
Assert.Equal(expected, RunTally(rows));
}

[Fact(Skip = "Remove to run test")]
public void A_win_is_three_points_a_loss_is_zero_points()
{
var expected =
"Team                           | MP |  W |  D |  L |  P\n" +
"Allegoric Alaskans             |  1 |  1 |  0 |  0 |  3\n" +
"Blithering Badgers             |  1 |  0 |  0 |  1 |  0";
Assert.Equal(expected, RunTally(rows));
}

[Fact(Skip = "Remove to run test")]
public void A_win_can_also_be_expressed_as_a_loss()
{
var expected =
"Team                           | MP |  W |  D |  L |  P\n" +
"Allegoric Alaskans             |  1 |  1 |  0 |  0 |  3\n" +
"Blithering Badgers             |  1 |  0 |  0 |  1 |  0";
Assert.Equal(expected, RunTally(rows));
}

[Fact(Skip = "Remove to run test")]
public void A_different_team_can_win()
{
var expected =
"Team                           | MP |  W |  D |  L |  P\n" +
"Blithering Badgers             |  1 |  1 |  0 |  0 |  3\n" +
"Allegoric Alaskans             |  1 |  0 |  0 |  1 |  0";
Assert.Equal(expected, RunTally(rows));
}

[Fact(Skip = "Remove to run test")]
public void A_draw_is_one_point_each()
{
var expected =
"Team                           | MP |  W |  D |  L |  P\n" +
"Allegoric Alaskans             |  1 |  0 |  1 |  0 |  1\n" +
"Blithering Badgers             |  1 |  0 |  1 |  0 |  1";
Assert.Equal(expected, RunTally(rows));
}

[Fact(Skip = "Remove to run test")]
public void There_can_be_more_than_one_match()
{
var rows =
var expected =
"Team                           | MP |  W |  D |  L |  P\n" +
"Allegoric Alaskans             |  2 |  2 |  0 |  0 |  6\n" +
"Blithering Badgers             |  2 |  0 |  0 |  2 |  0";
Assert.Equal(expected, RunTally(rows));
}

[Fact(Skip = "Remove to run test")]
public void There_can_be_more_than_one_winner()
{
var rows =
var expected =
"Team                           | MP |  W |  D |  L |  P\n" +
"Allegoric Alaskans             |  2 |  1 |  0 |  1 |  3\n" +
"Blithering Badgers             |  2 |  1 |  0 |  1 |  3";
Assert.Equal(expected, RunTally(rows));
}

[Fact(Skip = "Remove to run test")]
public void There_can_be_more_than_two_teams()
{
var rows =
var expected =
"Team                           | MP |  W |  D |  L |  P\n" +
"Allegoric Alaskans             |  2 |  2 |  0 |  0 |  6\n" +
"Blithering Badgers             |  2 |  1 |  0 |  1 |  3\n" +
"Courageous Californians        |  2 |  0 |  0 |  2 |  0";
Assert.Equal(expected, RunTally(rows));
}

[Fact(Skip = "Remove to run test")]
public void Typical_input()
{
var rows =
"Devastating Donkeys;Courageous Californians;draw\n" +
var expected =
"Team                           | MP |  W |  D |  L |  P\n" +
"Devastating Donkeys            |  3 |  2 |  1 |  0 |  7\n" +
"Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6\n" +
"Blithering Badgers             |  3 |  1 |  0 |  2 |  3\n" +
"Courageous Californians        |  3 |  0 |  1 |  2 |  1";
Assert.Equal(expected, RunTally(rows));
}

[Fact(Skip = "Remove to run test")]
public void Incomplete_competition_not_all_pairs_have_played_()
{
var rows =
var expected =
"Team                           | MP |  W |  D |  L |  P\n" +
"Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6\n" +
"Blithering Badgers             |  2 |  1 |  1 |  0 |  4\n" +
"Courageous Californians        |  2 |  0 |  1 |  1 |  1\n" +
"Devastating Donkeys            |  1 |  0 |  0 |  1 |  0";
Assert.Equal(expected, RunTally(rows));
}

[Fact(Skip = "Remove to run test")]
public void Ties_broken_alphabetically()
{
var rows =
"Courageous Californians;Devastating Donkeys;win\n" +
var expected =
"Team                           | MP |  W |  D |  L |  P\n" +
"Allegoric Alaskans             |  3 |  2 |  1 |  0 |  7\n" +
"Courageous Californians        |  3 |  2 |  1 |  0 |  7\n" +
"Blithering Badgers             |  3 |  0 |  1 |  2 |  1\n" +
"Devastating Donkeys            |  3 |  0 |  1 |  2 |  1";
Assert.Equal(expected, RunTally(rows));
}

private string RunTally(string input)
{
var encoding = new UTF8Encoding();
using (var inStream = new MemoryStream(encoding.GetBytes(input)))
using (var outStream = new MemoryStream())
{
Tournament.Tally(inStream, outStream);
return encoding.GetString(outStream.ToArray());
}
}
}``````
``````﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

public static class Tournament
{
private const string HEADER = "Team                           | MP |  W |  D |  L |  P";

private static List<Team> teams;

public static void Tally(Stream inStream, Stream outStream)
{
teams = new List<Team>();
using (var sr = new StreamReader(inStream))
{
string result;
while ((result = sr.ReadLine()) != null)
{
ApplyResult(result);
}
}

teams.Sort();
outStream.Write(Encoding.UTF8.GetBytes(GetTable()));
}

private static void ApplyResult(string result)
{
var parts = result.Split(";");
if (parts.Length != 3)
{
throw new ArgumentException();
}

var firstTeam = GetTeam(parts[0]);
var secondTeam = GetTeam(parts[1]);
switch (parts[2])
{
case "win":
firstTeam.Wins++;
secondTeam.Losses++;
break;
case "draw":
firstTeam.Draws++;
secondTeam.Draws++;
break;
case "loss":
firstTeam.Losses++;
secondTeam.Wins++;
break;
}
}

private static Team GetTeam(string name)
{
var team = teams.Where(t => t.Name.Equals(name)).FirstOrDefault();
if (team == null)
{
team = new Team(name);
}
return team;
}

private static string GetTable() => teams.Count == 0 ? HEADER
: HEADER + "\n" + string.Join('\n', teams.Select(t => t.ToString()));

private class Team : IEquatable<Team>, IComparable<Team>
{
public string Name { get; set; }
public int Wins { get; set; }
public int Draws { get; set; }
public int Losses { get; set; }

public Team(string name)
{
Name = name;
Wins = Draws = Losses = 0;
}

private int GetPoints() => 3 * Wins + Draws;

public override string ToString()
{
var matches = Wins + Draws + Losses;
return string.Format("{0,-30}{6,2}{1,3}{6,2}{2,3}{6,2}{3,3}{6,2}{4,3}{6,2}{5,3}",
Name, matches, Wins, Draws, Losses, GetPoints(), "|");
}

public override bool Equals(object obj) =>
(obj != null) && (obj is Team objAsTeam) && Equals(objAsTeam);

bool IEquatable<Team>.Equals(Team other) => Name.Equals(other.Name);

int IComparable<Team>.CompareTo(Team other)
{
int diff = other.GetPoints() - GetPoints();
return diff == 0 ? Name.CompareTo(other.Name) : diff;
}
}
}``````