elena1234

Snowwhite_withClass - ordered against the recomendation

Jan 10th, 2021 (edited)
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5.  
  6. namespace Snowwhite_withClass
  7. {
  8.     public class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var dictHatColourDwarf = new Dictionary<string, List<Dwarf>>();
  13.             string input;
  14.             while ((input = Console.ReadLine()) != "Once upon a time")
  15.             {
  16.                 string[] inputArray = input.Split(" <:> ");
  17.                 string name = inputArray[0];
  18.                 string hatColour = inputArray[1];
  19.                 int physic = int.Parse(inputArray[2]);
  20.                 if (!dictHatColourDwarf.ContainsKey(hatColour))
  21.                 {
  22.                     Dwarf newDwarf = new Dwarf(hatColour);
  23.                     newDwarf.HatColour = hatColour;
  24.                     newDwarf.Name = name;
  25.                     newDwarf.Physic = physic;
  26.                     dictHatColourDwarf[hatColour] = new List<Dwarf>();
  27.                     dictHatColourDwarf[hatColour].Add(newDwarf);
  28.                 }
  29.  
  30.                 else if (dictHatColourDwarf.ContainsKey(hatColour) && !dictHatColourDwarf[hatColour].Any(x => x.Name == name))
  31.                 {
  32.                     Dwarf newDwarf = new Dwarf(hatColour);
  33.                     newDwarf.HatColour = hatColour;
  34.                     newDwarf.Name = name;
  35.                     newDwarf.Physic = physic;
  36.                     dictHatColourDwarf[hatColour].Add(newDwarf);
  37.                 }
  38.  
  39.                 else if (dictHatColourDwarf.ContainsKey(hatColour) && dictHatColourDwarf[hatColour].Any(x => x.Name == name))
  40.                 {
  41.                     var findDwarf = dictHatColourDwarf[hatColour].First(x => x.Name == name);
  42.                     if (findDwarf.Physic < physic)
  43.                     {
  44.                         findDwarf.Physic = physic;
  45.                     }
  46.                 }
  47.             }
  48.  
  49.             PrintSortedDwarfs(dictHatColourDwarf);
  50.         }
  51.  
  52.  
  53.         private static void PrintSortedDwarfs(Dictionary<string, List<Dwarf>> dictHatColourDwarf)
  54.         {
  55.             var sortedDictByHatColour = new Dictionary<string, int>();
  56.             foreach (var (hatColour, listOfDwarfs) in dictHatColourDwarf.OrderByDescending(x => x.Value.Count))
  57.             {
  58.                 foreach (var dwarf in listOfDwarfs)
  59.                 {
  60.                     sortedDictByHatColour.Add($"({hatColour}) {dwarf.Name} <-> ", dwarf.Physic);
  61.                 }
  62.             }
  63.  
  64.             foreach (var (hatColorWithName, physic) in sortedDictByHatColour.OrderByDescending(x => x.Value))
  65.             {
  66.                 Console.WriteLine($"{hatColorWithName}{physic}");
  67.             }
  68.         }
  69.     }
  70.  
  71.  
  72.     public class Dwarf
  73.     {
  74.         public string HatColour { get; set; }
  75.         public string Name { get; set; }
  76.         public int Physic { get; set; }
  77.  
  78.         public Dwarf(string hatColour)
  79.         {
  80.             this.HatColour = hatColour;
  81.             this.Name = Name;
  82.             this.Physic = Physic;
  83.         }
  84.     }
  85. }
Add Comment
Please, Sign In to add comment