Advertisement
sivancheva

PopulationAggregation

Oct 16th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _04_PopulationAggregation
  9. {
  10. class PopulationAggregation
  11. {
  12. static void Main(string[] args)
  13. {
  14. var input = Console.ReadLine();
  15. var statePopulationDict = new Dictionary<string, List<TownPopulationInfo>>();
  16. var townPopulation = new Dictionary<string, long>();
  17.  
  18. while (input != "stop")
  19. {
  20. var inputArr = input.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
  21.  
  22. if (inputArr.Length != 3)
  23. {
  24. input = Console.ReadLine();
  25. continue;
  26. }
  27.  
  28. string cleanINputFirst = Regex.Replace(inputArr[0], "[^A-Za-z]", "");
  29. string cleanINputSecond = Regex.Replace(inputArr[1], "[^A-Za-z]", "");
  30.  
  31. string state = string.Empty;
  32. string town = string.Empty;
  33. long polulation = long.Parse(inputArr[2]);
  34.  
  35. if (char.IsUpper(cleanINputFirst[0]))
  36. {
  37. state = cleanINputFirst;
  38. town = cleanINputSecond;
  39. }
  40. else
  41. {
  42. state = cleanINputSecond;
  43. town = cleanINputFirst;
  44. }
  45.  
  46. var temp = new TownPopulationInfo();
  47. temp.Town = town;
  48. temp.Population = polulation;
  49.  
  50. if (!statePopulationDict.ContainsKey(state))
  51. {
  52. statePopulationDict.Add(state, new List<TownPopulationInfo>());
  53.  
  54. }
  55. statePopulationDict[state].Add(temp);
  56.  
  57. townPopulation[town] = polulation;
  58.  
  59.  
  60. input = Console.ReadLine();
  61.  
  62. }
  63.  
  64. var sortedDict = statePopulationDict
  65. .OrderBy(a => a.Key)
  66. .OrderByDescending(a => a.Value.Count)
  67. .ToArray();
  68.  
  69.  
  70.  
  71. foreach (var state in statePopulationDict.OrderBy(a => a.Key))
  72. {
  73. Console.WriteLine($"{state.Key} -> {state.Value.Count}");
  74.  
  75. }
  76.  
  77. foreach (var town in townPopulation.OrderByDescending(x => x.Value).Take(3))
  78. {
  79. Console.WriteLine($"{town.Key} -> {town.Value}");
  80. }
  81.  
  82. }
  83. class TownPopulationInfo
  84. {
  85. public string Town { get; set; }
  86. public long Population { get; set; }
  87.  
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement