-Annie-

PopulationCounter

Jun 12th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. namespace PopulationCounter
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class PopulationCounter
  8.     {
  9.         public static void Main()
  10.         {
  11.             Dictionary<string, Dictionary<string, double>> mydictionary = new Dictionary<string, Dictionary<string, double>>();
  12.  
  13.             string input = Console.ReadLine();
  14.  
  15.             while (input != "report")
  16.             {
  17.                 string[] inputArguments = input.Split('|');
  18.                 string city = inputArguments[0];
  19.                 string country = inputArguments[1];
  20.                 int population = int.Parse(inputArguments[2]);
  21.  
  22.                 if (!mydictionary.ContainsKey(country))
  23.                 {
  24.                     mydictionary.Add(country, new Dictionary<string, double>());
  25.                 }
  26.  
  27.                 mydictionary[country].Add(city, population);
  28.  
  29.                 input = Console.ReadLine();
  30.             }
  31.  
  32.             var orderedCountries = mydictionary.OrderByDescending(x=>x.Value.Values.Sum());
  33.  
  34.             foreach (var countryName in orderedCountries)
  35.             {
  36.                 Console.WriteLine($"{countryName.Key} (total population: {countryName.Value.Sum(x => x.Value)})");
  37.  
  38.                 var orderedCities = countryName.Value.OrderByDescending(x => x.Value);
  39.  
  40.                 foreach (var cityName in orderedCities)
  41.                 {
  42.                     Console.WriteLine($"=>{cityName.Key}: {cityName.Value}");
  43.                 }
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49. //Sofia|Bulgaria|1 000 000
  50. //Veliko Tarnovo|Bulgaria|2 000 000
  51. //London|UK|4 000 000
  52. //Rome|Italy|3 000 000
  53. //report
Advertisement
Add Comment
Please, Sign In to add comment