# trps's solution

## to Tournament in the C# Track

Published at Sep 25 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.

## 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]
public void Just_the_header_if_no_input()
{
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 rows = "Allegoric Alaskans;Blithering Badgers;win";
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 rows = "Blithering Badgers;Allegoric Alaskans;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 rows = "Blithering Badgers;Allegoric Alaskans;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 rows = "Allegoric Alaskans;Blithering Badgers;draw";
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 =
"Allegoric Alaskans;Blithering Badgers;win\n" +
"Allegoric Alaskans;Blithering Badgers;win";
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 =
"Allegoric Alaskans;Blithering Badgers;loss\n" +
"Allegoric Alaskans;Blithering Badgers;win";
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 =
"Allegoric Alaskans;Blithering Badgers;win\n" +
"Blithering Badgers;Courageous Californians;win\n" +
"Courageous Californians;Allegoric Alaskans;loss";
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 =
"Allegoric Alaskans;Blithering Badgers;win\n" +
"Devastating Donkeys;Courageous Californians;draw\n" +
"Devastating Donkeys;Allegoric Alaskans;win\n" +
"Courageous Californians;Blithering Badgers;loss\n" +
"Blithering Badgers;Devastating Donkeys;loss\n" +
"Allegoric Alaskans;Courageous Californians;win";
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 =
"Allegoric Alaskans;Blithering Badgers;loss\n" +
"Devastating Donkeys;Allegoric Alaskans;loss\n" +
"Courageous Californians;Blithering Badgers;draw\n" +
"Allegoric Alaskans;Courageous Californians;win";
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" +
"Allegoric Alaskans;Blithering Badgers;win\n" +
"Devastating Donkeys;Allegoric Alaskans;loss\n" +
"Courageous Californians;Blithering Badgers;win\n" +
"Blithering Badgers;Devastating Donkeys;draw\n" +
"Allegoric Alaskans;Courageous Californians;draw";
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
{
enum MatchState
{
Won,
Lost,
Draw
}
class Team
{
public Team(string name, MatchState ms)
{
TeamName = name;
UpdateStatistics(ms);
}
public void UpdateStatistics(MatchState ms)
{
MP++;
switch(ms)
{
case MatchState.Won:
W++;
break;
case MatchState.Lost:
L++;
break;
default:
D++;
break;
}
P = W * 3 + D;
}
public override string ToString()
{
char[] space = new char[31 - TeamName.Length];

for (int i = 0; i < space.Length; i++)
{
space[i] = ' ';
}
return string.Format(\$"{TeamName}{string.Join<char>("", space)}|  {MP} |  {W} |  {D} |  {L} |  {P}");
}
public string TeamName;
public int MP { get; private set; }
public int W { get; private set; }
public int D { get; private set; }
public int L { get; private set; }
public int P { get; private set; }
}

public static void Tally(Stream inStream, Stream outStream)
{
StreamReader sr = new StreamReader(inStream);
StreamWriter sw = new StreamWriter(outStream);

string input = sr.ReadToEnd();
List<Team> teams = new List<Team>();
string[] statements = input != "" ? input.Split('\n') : null;
string[] results;
MatchState ems;
for (int i = 0; i < statements?.Length; i++)
{
results = statements[i].Split(';');
if (results[2] == "win") { ems = MatchState.Won; }
else if (results[2] == "loss") { ems = MatchState.Lost; }
else { ems = MatchState.Draw; }

for (int z = 0; z < 2; z++)
{
if (teams.Any(team => team.TeamName == results[z]))
{
teams.Find(team => team.TeamName == results[z]).UpdateStatistics(ems);
}
else
{
teams.Add(new Team(results[z], ems));
}
if (ems == MatchState.Won) { ems = MatchState.Lost; }
else if (ems == MatchState.Lost) { ems = MatchState.Won; }
}
}
List<string> list = new List<string>();
list.Add("Team                           | MP |  W |  D |  L |  P");
string[] expected = teams.OrderByDescending(team => team.P).ThenBy(team => team.TeamName)
.Select(team => team.ToString()).ToArray();
list.InsertRange(1, expected);
string str = string.Join('\n', list.ToArray());
sw.Write(str);
sw.Flush();
sr.Dispose();
}
}``````

## 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?