Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CitiesByContinentAndCountry
  6. {
  7.     class StartUp
  8.     {
  9.         static void Main()
  10.         {
  11.             List<Continent> continents = new List<Continent>();
  12.  
  13.             int n = int.Parse(Console.ReadLine());
  14.  
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 string[] command = Console.ReadLine()
  18.                     .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  19.                     .ToArray();
  20.  
  21.                 string continent = command[0];
  22.                 string country = command[1];
  23.                 string city = command[2];
  24.  
  25.                 if (!continents.Any(x=>x.ContinentName == continent))
  26.                 {
  27.                     var currentContinent = new Continent(continent, new Country(country));
  28.                     currentContinent.Countries.Add(city)
  29.                     continents.Add(currentContinent);
  30.                 }
  31.                 else
  32.                 {
  33.                     var target = continents.FirstOrDefault(x => x.ContinentName == continent);
  34.  
  35.                     if (target.Country.CountryName == country)
  36.                     {
  37.                         target.Country.City.Add(city);
  38.                     }
  39.                     else
  40.                     {
  41.                         var currentContinent = new Continent(continent, new Country(country));
  42.                         currentContinent.Country.City.Add(city);
  43.                         continents.Add(currentContinent);
  44.                     }
  45.                 }
  46.             }
  47.  
  48.             foreach (var item in continents)
  49.             {
  50.                 Console.WriteLine(item.ContinentName);
  51.  
  52.                 Console.WriteLine($"{item.Country.CountryName} -> {string.Join(" ", item.Country.City)}");
  53.                
  54.             }
  55.         }
  56.  
  57.         public class Continent
  58.         {
  59.             public Continent(string continentName, Country country)
  60.             {
  61.                 this.ContinentName = continentName;
  62.                 this.Countries = new HashSet<Country>();
  63.             }
  64.  
  65.             public HashSet<Country> Countries { get; set; }
  66.  
  67.             public string ContinentName { get; set; }
  68.  
  69.             public void Add(Country country)
  70.             {
  71.                 Countries.Add(country);
  72.             }
  73.         }
  74.  
  75.         public class Country
  76.         {
  77.             public Country(string countryName)
  78.             {
  79.                 CountryName = countryName;
  80.                 City = new HashSet<string>();
  81.             }
  82.  
  83.             public string CountryName { get; set; }
  84.  
  85.             public HashSet<string> City { get; set; }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement