Advertisement
dimipan80

Population Counter

Mar 18th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1. /*
  2. So many people!
  3. It’s hard to count them all. But that’s your job as a statistician.
  4. You get raw data for a given city and you need to aggregate it.
  5. On each input line you’ll be given data in format: "city|country|population".
  6. There will be no redundant whitespaces anywhere in the input.
  7. Aggregate the data by country and by city and print it on the console.
  8. For each country, print its total population and on separate lines the data for each of its cities.
  9. Countries should be ordered by their total population in descending order and within each country,
  10. the cities should be ordered by the same criterion. If two countries/cities have the same population,
  11. keep them in the order in which they were entered.
  12. Input
  13. The input data should be read from the console.
  14. It consists of a variable number of lines and ends when the command "report" is received.
  15. The input data will always be valid and in the format described. There is no need to check it explicitly.
  16. Output
  17. The output should be printed on the console.
  18. Print the aggregated data for each country and city in the format shown below.
  19. Constraints
  20. The name of the city, country and the population count will be separated from each other by a pipe ('|').
  21. The number of input lines will be in the range [2 … 50].
  22. A city-country pair will not be repeated.
  23. The population count of each city will be an integer in the range [0 … 2 000 000 000].
  24. Allowed working time for your program: 0.1 seconds. Allowed memory: 16 MB.
  25. */
  26.  
  27. namespace Population_Statistics
  28. {
  29.     using System;
  30.     using System.Collections.Generic;
  31.     using System.Linq;
  32.  
  33.     internal static class PopulationCounter
  34.     {
  35.         private static void Main()
  36.         {
  37.             var townsInNations = new Dictionary<string, List<string>>();
  38.             var countryPopulation = new Dictionary<string, long>();
  39.             var cityPopulation = new Dictionary<string, long>();
  40.  
  41.             while (true)
  42.             {
  43.                 var inputData = Console.ReadLine().Split('|');
  44.                 if (inputData[0] == "report")
  45.                 {
  46.                     break;
  47.                 }
  48.  
  49.                 var city = inputData[0];
  50.                 var country = inputData[1];
  51.                 var population = long.Parse(inputData[2]);
  52.  
  53.                 if (!countryPopulation.ContainsKey(country))
  54.                 {
  55.                     countryPopulation.Add(country, 0);
  56.                 }
  57.  
  58.                 countryPopulation[country] += population;
  59.  
  60.                 if (!townsInNations.ContainsKey(country))
  61.                 {
  62.                     townsInNations.Add(country, new List<string>());
  63.                 }
  64.  
  65.                 if (!townsInNations[country].Contains(city))
  66.                 {
  67.                     townsInNations[country].Add(city);
  68.                 }
  69.  
  70.                 if (!cityPopulation.ContainsKey(city))
  71.                 {
  72.                     cityPopulation.Add(city, 0);
  73.                 }
  74.  
  75.                 cityPopulation[city] += population;
  76.             }
  77.  
  78.             List<string> orderedNations = countryPopulation
  79.                 .OrderByDescending(c => c.Value)
  80.                 .Select(c => c.Key)
  81.                 .ToList();
  82.  
  83.             List<string> orderedTowns = cityPopulation
  84.                 .OrderByDescending(t => t.Value)
  85.                 .Select(t => t.Key)
  86.                 .ToList();
  87.  
  88.             foreach (var country in orderedNations)
  89.             {
  90.                 Console.WriteLine(
  91.                     "{0} (total population: {1})", country, countryPopulation[country]);
  92.  
  93.                 foreach (var city in orderedTowns)
  94.                 {
  95.                     if (townsInNations[country].Contains(city))
  96.                     {
  97.                         Console.WriteLine("=>{0}: {1}", city, cityPopulation[city]);
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement