Advertisement
Aliendreamer

population counter

Jun 10th, 2018
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _7._Population_Counter
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, SortedDictionary<string, long>> totalPopulationInCountry = new Dictionary<string, SortedDictionary<string, long>>();
  12.             string input;
  13.             while ((input=Console.ReadLine())!="report")
  14.             {
  15.              
  16.                 string[] splitedInput = input?.Split(new []{'|'},StringSplitOptions.RemoveEmptyEntries).ToArray();
  17.  
  18.                 string city = splitedInput[0];
  19.                 string country = splitedInput[1];
  20.                 long populationInCity = long.Parse(splitedInput[2]);
  21.  
  22.  
  23.  
  24.                 if (!totalPopulationInCountry.ContainsKey(country))
  25.                 {
  26.                     totalPopulationInCountry.Add(country, new SortedDictionary<string, long>());
  27.                 }
  28.                 if (!totalPopulationInCountry[country].ContainsKey(city))
  29.                 {
  30.                     totalPopulationInCountry[country].Add(city, 0);
  31.                 }
  32.                 totalPopulationInCountry[country][city] = populationInCity;
  33.             }
  34.  
  35.  
  36.             foreach (var item in totalPopulationInCountry.OrderByDescending(x => x.Value.Values.Sum()))
  37.             {
  38.                 Console.WriteLine($"{item.Key} (total population: {item.Value.Values.Sum()})");
  39.                 foreach (var cities in item.Value.OrderByDescending(c => c.Value))
  40.                 {
  41.                     Console.WriteLine($"=>{cities.Key}: {cities.Value}");
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement