ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# freddie2025's solution

## to Tournament in the C# Track

Published at Mar 09 2020 · 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

``````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.

### TournamentTests.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 TournamentTests
{
[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.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;

public static class Tournament
{
enum Result
{
Loss = 0,
Draw = 1,
Win = 3
}

private static readonly Dictionary<string, (int played, int wins, int losses, int draws)> Results = new Dictionary<string, (int played, int wins, int losses, int draws)>();

private const int BlankSpace = 31;

public static void Tally(Stream inStream, Stream outStream)
{
Results.Clear();

{
while(!sr.EndOfStream)
{
var gameParts = game.Split(';');

string player1 = gameParts[0], player2 = gameParts[1];

Enum.TryParse(gameParts[2], true, out Result result);

}
}

using (StreamWriter sw = new StreamWriter(outStream))
{
sw.Write(GenerateResultsTable());
}
}

private static void AddResult(string player, Result result)
{
int played = 0, wins = 0, losses = 0, draws = 0;

if (Results.ContainsKey(player))
{
played = Results[player].played;
wins = Results[player].wins;
losses = Results[player].losses;
draws = Results[player].draws;
}
else
{
}

switch(result)
{
case Result.Win : Results[player] = (played +1, wins +1, losses, draws);
break;
case Result.Loss : Results[player] = (played +1, wins, losses +1, draws);
break;
case Result.Draw : Results[player] = (played +1, wins, losses, draws +1);
break;
}
}

private static Result SwitchResult(Result result)
{
switch (result)
{
case Result.Win :
return Result.Loss;
case Result.Loss :
return Result.Win;
default :
return Result.Draw;
}
}

private static string GenerateResultsTable()
{
var sb = new StringBuilder("Team                           | MP |  W |  D |  L |  P\n");

foreach(var kvp in Results.OrderByDescending(x => CalculateTotalPoints(x.Value.wins, x.Value.draws)).ThenBy(x => x.Key))
{
sb.Append(\$"{kvp.Key}{GenerateBlankSpace(kvp.Key.Count())}|  {kvp.Value.played} |  {kvp.Value.wins} |  {kvp.Value.draws} |  {kvp.Value.losses} |  {CalculateTotalPoints(kvp.Value.wins, kvp.Value.draws)}\n");
}

return sb.ToString().Trim('\n');
}

private static int CalculateTotalPoints(int wins, int draws)
{
return (wins * (int)Result.Win) + (draws * (int)Result.Draw);
}

private static string GenerateBlankSpace(int count)
{
return new string(' ', BlankSpace - count);
}
}``````