Advertisement
Dubwyn

Untitled

Mar 11th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace zad4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.            
  14.             var countrys = new Dictionary<string, List<string>>();
  15.             here:
  16.             var command = Console.ReadLine();
  17.             while(command != "End")
  18.             {
  19.                 var commandArgs = command.Split();
  20.                 if(commandArgs[0] == "Add")
  21.                 {
  22.                     var country = commandArgs[1];
  23.                     List<string> cities = commandArgs.Skip(2).ToList();
  24.                     if (!countrys.ContainsKey(country))
  25.                     {
  26.                         countrys.Add(commandArgs[1], new List<string>());
  27.                     }
  28.                     countrys[country].AddRange(cities);
  29.  
  30.                 }
  31.                 else if (commandArgs[0] == "Remove")
  32.                 {
  33.                     bool test = false;
  34.                     string cities = commandArgs[1];
  35.                     foreach (var country in countrys.Values)
  36.                     {
  37.                        
  38.                    
  39.                     if (country.Contains(cities))
  40.                     {
  41.                             country.Remove(cities);
  42.                             test = true;
  43.                             break;
  44.                     }
  45.                     else
  46.                     {
  47.                             test = false;
  48.                     }
  49.                     }
  50.  
  51.                     if (!test) Console.WriteLine($"City {commandArgs[1]} not found");
  52.                    
  53.                 }
  54.                
  55.                 goto here;
  56.             }
  57.  
  58.            
  59.             foreach (var country in countrys.OrderByDescending(x => x.Value.Count).ThenBy(x => x.Key))
  60.             {
  61.                
  62.                 string cities = string.Join(", ", country.Value);
  63.                 var numberOfCities = country.Value.Count;
  64.                 Console.WriteLine($"{country.Key} has {numberOfCities} cities and they are: {cities}");
  65.             }
  66.  
  67.         }
  68.     }
  69. }
  70. /*
  71. Add Germany Berlin
  72. Add USA Miami
  73. Add USA NewYork
  74. Add UK London
  75. Add UK Bristol
  76. Remove Kaspichan
  77. End
  78.  
  79. Add Germany Berlin
  80. Add Germany Hannover
  81. Add Bulgaria Sofia
  82. Add Bulgaria Plovdiv
  83. Remove Berlin
  84. Add Bulgaria Varna
  85. Add Bolivia Sucre
  86. End
  87.  
  88. Remove aa
  89. Remove bb
  90. Add cc c1
  91. End
  92. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement