Skip to content

Instantly share code, notes, and snippets.

@tstivers1990
Created December 29, 2018 19:49
Show Gist options
  • Save tstivers1990/fc76fc587e7bc89644bca66b20a12ca7 to your computer and use it in GitHub Desktop.
Save tstivers1990/fc76fc587e7bc89644bca66b20a12ca7 to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
// John responded to Michael 20 times
// John responded to Dave 10 times
// Michael responded to John 5 times
// Michael responded to Dave 8 times
// Dave responded to John 19 times
// Dave responded to Michael 3 times
var users = new Dictionary<string, User>
{
{"John", new User("John")},
{"Michael", new User("Michael")},
{"Dave", new User("Dave")}
};
users["John"].Replies["Michael"] = 20;
users["John"].Replies["Dave"] = 10;
users["Michael"].Replies["John"] = 5;
users["Michael"].Replies["Dave"] = 8;
users["Dave"].Replies["John"] = 19;
users["Dave"].Replies["Michael"] = 3;
var communicationTally = new Dictionary<(string, string), int>();
foreach (var a in users)
{
foreach (var b in a.Value.Replies)
{
var foundPair = false;
foreach (var cTallyKvp in communicationTally)
{
// Check if there is an already existing item in communicationTally that
// represents this pair of users.
if ((cTallyKvp.Key.Item1 == a.Key && cTallyKvp.Key.Item2 == b.Key) ||
(cTallyKvp.Key.Item1 == b.Key && cTallyKvp.Key.Item2 == a.Key))
{
foundPair = true;
communicationTally[cTallyKvp.Key] += b.Value;
break;
}
}
if (!foundPair)
{
communicationTally.Add((a.Key, b.Key), b.Value);
}
}
}
foreach (var kvp in communicationTally)
{
Console.WriteLine($"{kvp.Key.Item1} and {kvp.Key.Item2} communicated {kvp.Value} times.");
}
Console.WriteLine("Press the any key to continue...");
Console.ReadKey();
}
private class User
{
public string UserName { get; }
public Dictionary<string, int> Replies { get; }
public User(string userName)
{
UserName = userName;
Replies = new Dictionary<string, int>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment