joro_thexfiles

Ranking

Jul 28th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _4._Snowwhite
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var dict = new Dictionary<string, Dictionary<string, int>>();
  12.  
  13.             while (true)
  14.             {
  15.                 string text = Console.ReadLine();
  16.  
  17.                 if (text == "Once upon a time")
  18.                 {
  19.                     break;
  20.                 }
  21.                 else
  22.                 {
  23.                     string[] data = text
  24.                         .Split(" <:> ");
  25.  
  26.                     string name = data[0];
  27.                     string colorHat = data[1];
  28.                     int physics = int.Parse(data[2]);
  29.  
  30.                     if (!dict.ContainsKey(colorHat))
  31.                     {
  32.                         dict[colorHat] = new Dictionary<string, int>();
  33.                         dict[colorHat][name] = physics;
  34.                     }
  35.                     else
  36.                     {
  37.                         if (!dict[colorHat].ContainsKey(name))
  38.                         {
  39.                             dict[colorHat][name] = physics;
  40.                         }
  41.                         else
  42.                         {
  43.                             int currentPhysics = dict[colorHat][name];
  44.  
  45.                             if (currentPhysics < physics)
  46.                             {
  47.                                 dict[colorHat][name] = physics;
  48.                             }
  49.                         }
  50.                     }
  51.                 }          
  52.             }
  53.  
  54.             dict = dict
  55.                  .OrderByDescending(x => x.Value.Count())
  56.                  .ToDictionary(x => x.Key, z => z.Value);
  57.  
  58.             var sortedData = new Dictionary<string , int>();
  59.  
  60.             foreach (var kvp in dict)
  61.             {
  62.                 foreach (var item in kvp.Value)
  63.                 {
  64.                     string currentName = $"({kvp.Key}) {item.Key} <->";
  65.                     sortedData[currentName] = item.Value;
  66.                 }
  67.             }
  68.  
  69.             foreach (var kvp in sortedData
  70.                 .OrderByDescending(x=>x.Value))
  71.             {
  72.                 Console.WriteLine($"{kvp.Key} {kvp.Value}");
  73.             }
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment