Created
July 12, 2019 16:40
-
-
Save Retterath/f0a1ea61d209eef219bda9934764642a to your computer and use it in GitHub Desktop.
Time for some music, right?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace December2018 | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
var bandNameAndMembers = new Dictionary<string, List<string>>(); // first collection | |
var bandNameAndTime = new Dictionary<string, int>(); | |
int totalTimeCount = 0; // store each time everytime we get an input of time | |
string input = string.Empty; | |
while ((input = Console.ReadLine()) != "start of concert") | |
{ | |
// "Add; {bandName}; {member 1}, {member 2}, {member 3}" | |
string[] tokens = input.Split(new string[] { "; ", ", ", }, StringSplitOptions.RemoveEmptyEntries); | |
string bandName = tokens[1]; | |
if (tokens[0] == "Add") | |
{ | |
var listOfMembers = new List<string>(); | |
if (!bandNameAndMembers.ContainsKey(bandName)) | |
{ | |
bandNameAndMembers[bandName] = listOfMembers; | |
//bandNameAndMembers.Add(bandName, new List<string>()); | |
} | |
for (int i = 2; i < tokens.Length; i++) | |
{ | |
if (!bandNameAndMembers[bandName].Contains(tokens[i])) // check if each name in the list is present | |
{ | |
bandNameAndMembers[bandName].Add(tokens[i]); | |
} | |
} | |
} | |
else if (tokens[0] == "Play") | |
{ | |
int playTime = int.Parse(tokens[2]); | |
if (!bandNameAndTime.ContainsKey(bandName)) | |
{ | |
bandNameAndTime.Add(bandName, playTime); | |
} | |
else | |
{ | |
bandNameAndTime[bandName] += playTime; | |
} | |
} | |
} | |
foreach (var kvp in bandNameAndMembers) | |
{ | |
Console.WriteLine($"{kvp.Key} : {kvp.Value}"); | |
} | |
foreach (var kvp in bandNameAndTime) | |
{ | |
totalTimeCount += kvp.Value; // to store all time in one value, sum it up | |
} | |
string finalInput = Console.ReadLine(); | |
Console.WriteLine($"Total time: {totalTimeCount}"); | |
// time to sort the dictionaries | |
foreach (var item in bandNameAndTime.OrderByDescending(x => x.Value).ThenBy(x => x.Key)) // first by time, then by name | |
{ | |
Console.WriteLine($"{item.Key} => {item.Value}"); | |
} | |
Console.WriteLine(finalInput); | |
foreach (var band in bandNameAndMembers) | |
{ | |
if (band.Key == finalInput) | |
{ | |
// print the list and all its band members | |
// access the list via .Value, since its " new Dictionary<string, List<string>>(); " | |
foreach (var member in band.Value) | |
{ | |
Console.WriteLine($"=> {member}"); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment