Advertisement
YavorGrancharov

Population_Counter(dict)

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