Advertisement
Guest User

07. Population Counter

a guest
Jun 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _07_PopulationCounter
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var countries = new Dictionary<string, Dictionary<string, int>>();
  12.             var countryPopulation = new Dictionary<string, int>();
  13.  
  14.             while (true)
  15.             {
  16.                 string[] input = Console.ReadLine().Split("|");
  17.  
  18.                 if (input[0] == "report")
  19.                 {
  20.                     break;
  21.                 }
  22.  
  23.                 string country = input[1];
  24.                 string city = input[0];
  25.                 int population = int.Parse(input[2]);
  26.  
  27.                 if (countries.ContainsKey(country) == false)
  28.                 {
  29.                     countries.Add(country, new Dictionary<string, int>());
  30.                 }
  31.  
  32.                 if (countries[country].ContainsKey(city) == false)
  33.                 {
  34.                     countries[country].Add(city, 0);
  35.                 }
  36.  
  37.                 countries[country][city] += population;
  38.  
  39.  
  40.                 if (countryPopulation.ContainsKey(country) == false)
  41.                 {
  42.                     countryPopulation.Add(country, 0);
  43.                 }
  44.  
  45.                 countryPopulation[country] += population;
  46.             }
  47.  
  48.  
  49.             countryPopulation = countryPopulation
  50.                 .OrderByDescending(country => country.Value)
  51.                 .ToDictionary(country => country.Key , popul => popul.Value);
  52.  
  53.             foreach (var country in countryPopulation)
  54.             {
  55.                 string countryStr = country.Key.ToString();
  56.                 countries[countryStr] = countries[countryStr]
  57.                     .OrderByDescending(city => city.Value)
  58.                     .ToDictionary(city => city.Key, popul => popul.Value);
  59.  
  60.                 Console.WriteLine($"{countryStr} (total population: {country.Value})");
  61.  
  62.                 foreach (var kvp in countries[countryStr])
  63.                 {
  64.                     Console.WriteLine($"=>{kvp.Key}: {kvp.Value}");
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement