zdravko7

[C# Advanced Exam] Population Counter

Jul 19th, 2015
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. class PopulationCounter
  7. {
  8.     private static void Main()
  9.     {
  10.         Dictionary<string, Dictionary<string, long>> countries = new Dictionary<string, Dictionary<string, long>>();
  11.  
  12.         while (true)
  13.         {
  14.             string[] input = Console.ReadLine().Split(new char[] { '|' });
  15.  
  16.             if (input[0] == "report")
  17.                 break;
  18.  
  19.             string town = input[0];
  20.             string country = input[1];
  21.             int population = int.Parse(input[2]);
  22.  
  23.             if (!countries.ContainsKey(country))
  24.             {
  25.                 countries.Add(country, new Dictionary<string, long>());
  26.             }
  27.  
  28.             countries[country].Add(town, population);
  29.         }
  30.         var sortedCountries =
  31.             countries.OrderByDescending(x => x.Value.Values.Sum());
  32.  
  33.         foreach (var country in sortedCountries)
  34.         {
  35.             Console.Write("{0} ", country.Key);
  36.             Console.WriteLine("(total population: {0})", country.Value.Values.Sum());
  37.  
  38.             var sortedTowns = from entry in country.Value orderby entry.Value descending select entry;
  39.  
  40.             foreach (var town in sortedTowns)
  41.             {
  42.                 Console.WriteLine("=>{0}: {1}", town.Key, town.Value);
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment