Advertisement
bacco

Population Counter

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