Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- So many people!
- It’s hard to count them all. But that’s your job as a statistician.
- You get raw data for a given city and you need to aggregate it.
- On each input line you’ll be given data in format: "city|country|population".
- There will be no redundant whitespaces anywhere in the input.
- Aggregate the data by country and by city and print it on the console.
- For each country, print its total population and on separate lines the data for each of its cities.
- Countries should be ordered by their total population in descending order and within each country,
- the cities should be ordered by the same criterion. If two countries/cities have the same population,
- keep them in the order in which they were entered.
- Input
- The input data should be read from the console.
- It consists of a variable number of lines and ends when the command "report" is received.
- The input data will always be valid and in the format described. There is no need to check it explicitly.
- Output
- The output should be printed on the console.
- Print the aggregated data for each country and city in the format shown below.
- Constraints
- The name of the city, country and the population count will be separated from each other by a pipe ('|').
- The number of input lines will be in the range [2 … 50].
- A city-country pair will not be repeated.
- The population count of each city will be an integer in the range [0 … 2 000 000 000].
- Allowed working time for your program: 0.1 seconds. Allowed memory: 16 MB.
- */
- namespace Population_Statistics
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- internal static class PopulationCounter
- {
- private static void Main()
- {
- var townsInNations = new Dictionary<string, List<string>>();
- var countryPopulation = new Dictionary<string, long>();
- var cityPopulation = new Dictionary<string, long>();
- while (true)
- {
- var inputData = Console.ReadLine().Split('|');
- if (inputData[0] == "report")
- {
- break;
- }
- var city = inputData[0];
- var country = inputData[1];
- var population = long.Parse(inputData[2]);
- if (!countryPopulation.ContainsKey(country))
- {
- countryPopulation.Add(country, 0);
- }
- countryPopulation[country] += population;
- if (!townsInNations.ContainsKey(country))
- {
- townsInNations.Add(country, new List<string>());
- }
- if (!townsInNations[country].Contains(city))
- {
- townsInNations[country].Add(city);
- }
- if (!cityPopulation.ContainsKey(city))
- {
- cityPopulation.Add(city, 0);
- }
- cityPopulation[city] += population;
- }
- List<string> orderedNations = countryPopulation
- .OrderByDescending(c => c.Value)
- .Select(c => c.Key)
- .ToList();
- List<string> orderedTowns = cityPopulation
- .OrderByDescending(t => t.Value)
- .Select(t => t.Key)
- .ToList();
- foreach (var country in orderedNations)
- {
- Console.WriteLine(
- "{0} (total population: {1})", country, countryPopulation[country]);
- foreach (var city in orderedTowns)
- {
- if (townsInNations[country].Contains(city))
- {
- Console.WriteLine("=>{0}: {1}", city, cityPopulation[city]);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement