Guest User

PopulationCounter_SoftUni

a guest
Jun 1st, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 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. namespace _10.PopulationCounter
  8. {
  9.     class PopulationCounter
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             FillDictionary();
  14.         }
  15.  
  16.         static void FillDictionary()
  17.         {
  18.             Dictionary<string, Dictionary<string, long>> popCounter = new Dictionary<string, Dictionary<string, long>>();
  19.             string input = Console.ReadLine();
  20.             while (input != "report")
  21.             {
  22.                 string[] inputArray = input.Split('|');
  23.                 if (!popCounter.ContainsKey(inputArray[1]))
  24.                 {
  25.                     popCounter.Add(inputArray[1], new Dictionary<string, long>());
  26.                 }
  27.                 if (!popCounter[inputArray[1]].ContainsKey(inputArray[0]))
  28.                 {
  29.                     popCounter[inputArray[1]].Add(inputArray[0], 0);
  30.                 }
  31.                 popCounter[inputArray[1]][inputArray[0]] += long.Parse(inputArray[2]);
  32.                 input = Console.ReadLine();
  33.             }
  34.  
  35.             WriteOutput(popCounter);
  36.         }
  37.  
  38.         static void WriteOutput(Dictionary<string, Dictionary<string, long>> popCounter)
  39.         {
  40.             var myList = popCounter.ToList();
  41.             myList.Sort((pair1, pair2) => pair2.Value.Sum(x => x.Value).CompareTo(pair1.Value.Sum(x => x.Value)));
  42.  
  43.             foreach (KeyValuePair<string, Dictionary<string, long>> kvp1 in myList)
  44.             {
  45.                 Console.WriteLine("{0} (total population: {1})", kvp1.Key, kvp1.Value.Sum(x => x.Value));
  46.                 foreach (KeyValuePair<string, long> kvp2 in kvp1.Value.OrderByDescending(x => x.Value))
  47.                 {
  48.                     Console.WriteLine("=>{0}: {1}", kvp2.Key, kvp2.Value);
  49.                 }
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment