LardaX

Population_Counter

Oct 8th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 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 Population_Counter
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, Dictionary<string, long>> citiesInformation = new Dictionary<string, Dictionary<string, long>>();
  14.             string[] cityInformation = Console.ReadLine().Split('|').ToArray();
  15.  
  16.             while (!cityInformation[0].Equals("report"))
  17.             {
  18.                 string country = cityInformation[1];
  19.                 string city = cityInformation[0];
  20.                 long populationCount = long.Parse(cityInformation[2]);
  21.  
  22.                 InsertCountry(citiesInformation, country);
  23.                 InsertCityPopulation(citiesInformation, country, city, populationCount);
  24.  
  25.                 cityInformation = Console.ReadLine().Split('|').ToArray();
  26.             }
  27.  
  28.             PrintCityInformation(citiesInformation);
  29.         }
  30.  
  31.  
  32.  
  33.         private static void PrintCityInformation(Dictionary<string, Dictionary<string, long>> citiesInformation)
  34.         {
  35.             foreach (KeyValuePair<string, Dictionary<string, long>> cityEntry in citiesInformation.OrderByDescending(x => x.Value.Values.Sum()))
  36.             {
  37.                 Console.WriteLine($"{cityEntry.Key} (total population: {cityEntry.Value.Values.Sum()})");
  38.                 foreach (KeyValuePair<string, long> populationEntry in cityEntry.Value.OrderByDescending(x => x.Value))
  39.                 {
  40.                     Console.WriteLine($"=>{populationEntry.Key}: {populationEntry.Value}");
  41.                 }
  42.             }
  43.         }
  44.  
  45.         private static void InsertCityPopulation(Dictionary<string, Dictionary<string, long>> citiesInformation, string country, string city, long populationCount)
  46.         {
  47.             if (!citiesInformation[country].ContainsKey(city))
  48.             {
  49.                 citiesInformation[country].Add(city, 0);
  50.             }
  51.  
  52.             citiesInformation[country][city] += populationCount;
  53.         }
  54.  
  55.         private static void InsertCountry(Dictionary<string, Dictionary<string, long>> citiesInformation, string country)
  56.         {
  57.             if (!citiesInformation.ContainsKey(country))
  58.             {
  59.                 citiesInformation.Add(country, new Dictionary<string, long>());
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment