Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class Population_Counter
- {
- static void Main()
- {
- Dictionary<string, Dictionary<string, int>> populations = new Dictionary<string, Dictionary<string, int>>();
- string city = "";
- string country = "";
- int population = 0;
- while (true)
- {
- List<string> data = Console.ReadLine().Split('|').ToList();
- city = data[0];
- if (city == "report") break;
- country = data[1];
- population = int.Parse(data[2]);
- Dictionary<string, int> cityPopulation = new Dictionary<string, int>();
- cityPopulation[city] = population;
- if (!populations.ContainsKey(country))
- {
- populations[country] = cityPopulation;
- }
- else
- {
- populations[country].Add(city,population);
- }
- }
- foreach (var state in populations.OrderByDescending(x=>x.Key))
- {
- List<int> sumOfTowns = state.Value.Select(x => x.Value).ToList();
- Console.WriteLine($"{state.Key} (total population: {sumOfTowns.Sum()})");
- Console.Write($"=>{string.Join("=>", state.Value.Select(x => $"{x.Key}: {x.Value}\r\n").OrderByDescending(x=>x))}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement