Advertisement
YavorJS

Population Counter ordering problem

Oct 9th, 2016
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. class Population_Counter
  9. {
  10. static void Main()
  11. {
  12. Dictionary<string, Dictionary<string, int>> populations = new Dictionary<string, Dictionary<string, int>>();
  13.  
  14. string city = "";
  15. string country = "";
  16. int population = 0;
  17. while (true)
  18. {
  19. List<string> data = Console.ReadLine().Split('|').ToList();
  20. city = data[0];
  21. if (city == "report") break;
  22. country = data[1];
  23. population = int.Parse(data[2]);
  24. Dictionary<string, int> cityPopulation = new Dictionary<string, int>();
  25. cityPopulation[city] = population;
  26. if (!populations.ContainsKey(country))
  27. {
  28. populations[country] = cityPopulation;
  29. }
  30. else
  31. {
  32. populations[country].Add(city,population);
  33. }
  34. }
  35.  
  36. foreach (var state in populations.OrderByDescending(x=>x.Key))
  37. {
  38. List<int> sumOfTowns = state.Value.Select(x => x.Value).ToList();
  39. Console.WriteLine($"{state.Key} (total population: {sumOfTowns.Sum()})");
  40. Console.Write($"=>{string.Join("=>", state.Value.Select(x => $"{x.Key}: {x.Value}\r\n").OrderByDescending(x=>x))}");
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement