AreWe

03. Nikulden’s meals

Jul 30th, 2020 (edited)
1,343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleApp33
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args) {
  10.             Dictionary<string, List<string>> guests = new Dictionary<string, List<string>>();
  11.             int unliked = 0;
  12.  
  13.             string[] cmd;
  14.             while ((cmd = Console.ReadLine().Split('-'))[0] != "Stop") {
  15.                 string guest = cmd[1], meal = cmd[2];
  16.                 if (cmd[0] == "Like") {
  17.                     if (!guests.ContainsKey(guest)) {
  18.                         guests[guest] = new List<string> { meal };
  19.                     } else if (guests[guest].IndexOf(meal) == -1) {
  20.                         guests[guest].Add(meal);
  21.                     }
  22.                 } else { // Unlike
  23.                     int index;
  24.                     if (!guests.ContainsKey(guest)) {
  25.                         Console.WriteLine($"{guest} is not at the party.");
  26.                     } else if((index = guests[guest].IndexOf(meal)) == -1) {
  27.                         Console.WriteLine($"{guest} doesn't have the {meal} in his/her collection.");
  28.                     } else {
  29.                         guests[guest].RemoveAt(index);
  30.                         unliked++;
  31.                         Console.WriteLine($"{guest} doesn't like the {meal}.");
  32.                     }
  33.                 }
  34.             }
  35.  
  36.             guests
  37.                 .OrderByDescending(kvp => kvp.Value.Count)
  38.                 .ThenBy(kvp => kvp.Key)
  39.                 .ToList()
  40.                 .ForEach(kvp => {
  41.                     Console.WriteLine($"{kvp.Key}: {string.Join(", ", kvp.Value)}".Trim());
  42.                 });
  43.  
  44.             Console.WriteLine($"Unliked meals: {unliked}");
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment