Aliendreamer

dict 7

Feb 17th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         var population = new Dictionary<string, Dictionary<string, long>>();
  11.  
  12.         string input = Console.ReadLine();
  13.  
  14.         while (input != "report")
  15.         {
  16.             string[] tokens = input.Split(new[] {'|'}).ToArray();
  17.             string city = tokens[0];
  18.             string country = tokens[1];
  19.             long populations = long.Parse(tokens[2]);
  20.             if (!population.ContainsKey(country))
  21.             {
  22.                 population.Add(country, new Dictionary<string, long>());
  23.                 population[country].Add(city, populations);
  24.             }
  25.             else
  26.             {
  27.                 if (!population[country].ContainsKey(city))
  28.                 {
  29.                     population[country].Add(city, populations);
  30.                 }
  31.             }
  32.  
  33.             input = Console.ReadLine();
  34.  
  35.         }
  36.  
  37.      
  38.        
  39.         foreach (var countries in population
  40.             .OrderByDescending(x=>x.Value.Values.Sum()))
  41.         {
  42.             Console.WriteLine($"{countries.Key}: ");
  43.             foreach (var cities in countries.Value.OrderByDescending(x=>x.Value))
  44.             {
  45.                 Console.WriteLine($"=>{cities.Key}: {cities.Value} ");
  46.             }
  47.         }
  48.  
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment