TonyTroev

PopulationCounter

Dec 17th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 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 _14_Populations
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, Dictionary<string, long>> countries = new Dictionary<string, Dictionary<string, long>>();
  14.             while (true)
  15.             {
  16.                 string[] input = Console.ReadLine().Split('|');
  17.                 if (input[0] == "report")
  18.                 {
  19.                     break;
  20.                 }
  21.                 else
  22.                 {
  23.                     string country = input[0];
  24.                     string city = input[1];
  25.                     long people = long.Parse(input[2]);
  26.                     if (countries.ContainsKey(country))
  27.                     {
  28.                         countries[country].Add(city, people);
  29.                     }
  30.                     else
  31.                     {
  32.                         Dictionary<string, long> dictionary = new Dictionary<string, long>();
  33.                         dictionary.Add(city, people);
  34.                         countries.Add(country, dictionary);
  35.                     }
  36.                 }
  37.             }
  38.             var orderedDict = countries.OrderByDescending(pair => pair.Value.Values.Sum()).ToDictionary(x => x.Key, x => x.Value);
  39.             foreach (var pair in orderedDict)
  40.             {
  41.                 var country = pair.Key;
  42.                 var cities = pair.Value;
  43.                 var orderedCities = cities
  44.                     .OrderByDescending(x => x.Value)
  45.                     .ToDictionary(x => x.Key, x => x.Value);
  46.  
  47.  
  48.                 Console.WriteLine($"{country} (total population: {orderedCities.Values.Sum()})");
  49.  
  50.                 foreach (var nestedPair in orderedCities)
  51.                 {
  52.                     var city = nestedPair.Key;
  53.                     var cityPopulation = nestedPair.Value;
  54.                     Console.WriteLine($"=>{city}: {cityPopulation}");
  55.                 }
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment