Advertisement
Guest User

Population Counter

a guest
Oct 11th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class PopulationCounter
  6. {
  7. public static void Main()
  8. {
  9. string input = Console.ReadLine();
  10.  
  11. Dictionary<string, Dictionary<string, long>> countriesData = new Dictionary<string, Dictionary<string, long>>();
  12.  
  13. while (input != "report")
  14. {
  15. string[] rawData = input.Split('|');
  16. string country = rawData[1];
  17. string city = rawData[0];
  18. long population = long.Parse(rawData[2]);
  19.  
  20. if(!countriesData.ContainsKey(country))
  21. {
  22. countriesData[country] = new Dictionary<string, long>();
  23. }
  24. if(!countriesData[country].ContainsKey(city))
  25. {
  26. countriesData[country][city] = population;
  27. }
  28. else
  29. {
  30. countriesData[country][city] += population;
  31. }
  32.  
  33. input = Console.ReadLine();
  34. }
  35.  
  36. Dictionary<string, long> countryTotalPop = new Dictionary<string, long>();
  37.  
  38. foreach (KeyValuePair<string, Dictionary<string, long>> state in countriesData)
  39. {
  40. long totalPopulation = 0;
  41.  
  42. foreach (KeyValuePair<string, long> pair in state.Value)
  43. {
  44. totalPopulation += pair.Value;
  45. }
  46.  
  47. countryTotalPop[state.Key] = totalPopulation;
  48. }
  49.  
  50. foreach (KeyValuePair<string, long> state in countryTotalPop.OrderByDescending(x => x.Value))
  51. {
  52. Console.WriteLine($"{state.Key} (total population: {state.Value})");
  53.  
  54. foreach (KeyValuePair<string, Dictionary<string, long>> c in countriesData)
  55. {
  56. if (c.Key == state.Key)
  57. {
  58. foreach (KeyValuePair<string, long> pair in c.Value.OrderByDescending(x => x.Value))
  59. {
  60. Console.WriteLine($"=>{pair.Key}: {pair.Value}");
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement