Skip to content

Instantly share code, notes, and snippets.

@Retterath
Created August 1, 2019 12:47
Show Gist options
  • Save Retterath/c6df9255a550c902d5ee6c15e6da6219 to your computer and use it in GitHub Desktop.
Save Retterath/c6df9255a550c902d5ee6c15e6da6219 to your computer and use it in GitHub Desktop.
Code for a concert organisation/Can be improved with private functions/Comments included
namespace ExamPreparation
{
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
var bandAndMembers = new Dictionary<string, List<string>>();
var bandAndTime = new Dictionary<string, int>();
int totalTime = 0; // For all the time the bands have played
string input = string.Empty;
while ((input = Console.ReadLine()) != "start of concert")
{
string[] tokens = input.Split("; ", StringSplitOptions.RemoveEmptyEntries);
string command = tokens[0];
string bandName = tokens[1];
string members = tokens[2];
string[] allMembers = members.Split(", ");
switch (command)
{
case "Add":
if (!bandAndMembers.ContainsKey(bandName))
{
// All members must be unique so don't add duplicates
bandAndMembers[bandName] = new List<string>();
// bandAndMembers[bandName].Add(members);
for (int i = 0; i < allMembers.Length; i++)
{
string currentMember = allMembers[i];
if (!bandAndMembers[bandName].Contains(currentMember))
{
bandAndMembers[bandName].Add(currentMember);
}
}
}
else if (bandAndMembers.ContainsKey(bandName))
{
// add only those members that aren't present in the list.
for (int i = 0; i < allMembers.Length; i++)
{
string currentMember = allMembers[i];
if (!bandAndMembers[bandName].Contains(currentMember))
{
bandAndMembers[bandName].Add(currentMember);
}
}
}
break;
case "Play":
int time = int.Parse(tokens[2]);
if (!bandAndTime.ContainsKey(bandName))
{
totalTime += time;
bandAndTime[bandName] = time;
}
// a band that has already applied in the concert, just increase the band time
else
{
totalTime += time;
bandAndTime[bandName] += time;
}
break;
default:
break;
}
}
string finalInput = Console.ReadLine();
Console.WriteLine($"Total time: {totalTime}");
// the bands ordered by the time on stage in descending order, then by band name in ascending order
foreach (var (key,value) in bandAndTime.OrderByDescending(time=>time.Value).ThenBy(name=>name.Key))
{
Console.WriteLine($"{key} -> {value}");
}
// the final input line will be "{bandName}" and you have to print all members for this band in insertion order
Console.WriteLine(finalInput);
foreach (var members in bandAndMembers[finalInput])
{
Console.WriteLine($"=> {members}");
}
}
}
}
Play; The Beatles; 2584
Add; The Beatles; John Lennon, Paul McCartney, George Harrison, Ringo Starr
Add; Eagles; Glenn Frey, Don Henley, Bernie Leadon, Randy Meisner
Play; Eagles; 1869
Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards
Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards, Bill Wyman, Charlie Watts, Ian Stewart
Play; The Rolling Stones; 4239
start of concert
The Rolling Stones
OUTPUT:
Total time: 8692
The Rolling Stones -> 4239
The Beatles -> 2584
Eagles -> 1869
The Rolling Stones
=> Brian Jones
=> Mick Jagger
=> Keith Richards
=> Bill Wyman
=> Charlie Watts
=> Ian Stewart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment