Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SnowWhite
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Dwarf> dwarvesList = new List<Dwarf>();
- while (true)
- {
- string strInput = Console.ReadLine();
- if (strInput == "Once upon a time")
- {
- // print the output
- IEnumerable<IGrouping<string, Dwarf>> dwarvesOrdered =
- from dwarf in dwarvesList
- orderby dwarf.Physics descending
- group dwarf by dwarf.HatColour into dwarvesGroups
- orderby dwarvesGroups.Count() descending
- select dwarvesGroups;
- foreach (IGrouping<string, Dwarf> item in dwarvesOrdered)
- {
- foreach (Dwarf dwarf in item)
- {
- Console.WriteLine($"({item.Key}) {dwarf.Name} <-> {dwarf.Physics}");
- }
- }
- break;
- }
- string[] strSeparator = new string[] { " <:> " };
- string[] strInputArray = strInput.Split(strSeparator,
- StringSplitOptions.RemoveEmptyEntries);
- Dwarf clsDwarf = new Dwarf(strInputArray);
- string strName = strInputArray[0];
- string strHatColour = strInputArray[1];
- int intPhysics = int.Parse(strInputArray[2]);
- // check whether there is a dwarf with the same name
- Dwarf dwarfExists = dwarvesList.FirstOrDefault(
- dwarf => dwarf.Name == strName);
- if (dwarfExists != null) // exists
- {
- if (dwarfExists.HatColour != strHatColour)
- {
- dwarvesList.Add(clsDwarf);
- }
- else if (dwarfExists.HatColour == strHatColour)
- {
- if (dwarfExists.Physics < intPhysics)
- {
- dwarvesList.Remove(dwarfExists);
- dwarvesList.Add(clsDwarf);
- }
- }
- }
- else // doesn't exists
- {
- dwarvesList.Add(clsDwarf);
- }
- }
- }
- }
- class Dwarf
- {
- public Dwarf() { }
- public Dwarf(string[] strNameHatColourPhysics)
- {
- Name = strNameHatColourPhysics[0];
- HatColour = strNameHatColourPhysics[1];
- Physics = int.Parse(strNameHatColourPhysics[2]);
- }
- public string Name { get; set; }
- public string HatColour { get; set; }
- public int Physics { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement