kalitarix

Population Counter

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