Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace PopulationCounter
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class PopulationCounter
- {
- public static void Main()
- {
- Dictionary<string, Dictionary<string, double>> mydictionary = new Dictionary<string, Dictionary<string, double>>();
- string input = Console.ReadLine();
- while (input != "report")
- {
- string[] inputArguments = input.Split('|');
- string city = inputArguments[0];
- string country = inputArguments[1];
- int population = int.Parse(inputArguments[2]);
- if (!mydictionary.ContainsKey(country))
- {
- mydictionary.Add(country, new Dictionary<string, double>());
- }
- mydictionary[country].Add(city, population);
- input = Console.ReadLine();
- }
- var orderedCountries = mydictionary.OrderByDescending(x=>x.Value.Values.Sum());
- foreach (var countryName in orderedCountries)
- {
- Console.WriteLine($"{countryName.Key} (total population: {countryName.Value.Sum(x => x.Value)})");
- var orderedCities = countryName.Value.OrderByDescending(x => x.Value);
- foreach (var cityName in orderedCities)
- {
- Console.WriteLine($"=>{cityName.Key}: {cityName.Value}");
- }
- }
- }
- }
- }
- //Sofia|Bulgaria|1 000 000
- //Veliko Tarnovo|Bulgaria|2 000 000
- //London|UK|4 000 000
- //Rome|Italy|3 000 000
- //report
Advertisement
Add Comment
Please, Sign In to add comment