Advertisement
KAMEN1973

Untitled

Mar 20th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 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 SnowWhite
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<Dwarf> dwarvesList = new List<Dwarf>();
  14.  
  15. while (true)
  16. {
  17. string strInput = Console.ReadLine();
  18. if (strInput == "Once upon a time")
  19. {
  20. // print the output
  21. IEnumerable<IGrouping<string, Dwarf>> dwarvesOrdered =
  22. from dwarf in dwarvesList
  23. orderby dwarf.Physics descending
  24. group dwarf by dwarf.HatColour into dwarvesGroups
  25. orderby dwarvesGroups.Count() descending
  26. select dwarvesGroups;
  27. foreach (IGrouping<string, Dwarf> item in dwarvesOrdered)
  28. {
  29. foreach (Dwarf dwarf in item)
  30. {
  31. Console.WriteLine($"({item.Key}) {dwarf.Name} <-> {dwarf.Physics}");
  32. }
  33.  
  34. }
  35. break;
  36. }
  37. string[] strSeparator = new string[] { " <:> " };
  38. string[] strInputArray = strInput.Split(strSeparator,
  39. StringSplitOptions.RemoveEmptyEntries);
  40.  
  41. Dwarf clsDwarf = new Dwarf(strInputArray);
  42. string strName = strInputArray[0];
  43. string strHatColour = strInputArray[1];
  44. int intPhysics = int.Parse(strInputArray[2]);
  45.  
  46. // check whether there is a dwarf with the same name
  47. Dwarf dwarfExists = dwarvesList.FirstOrDefault(
  48. dwarf => dwarf.Name == strName);
  49. if (dwarfExists != null) // exists
  50. {
  51. if (dwarfExists.HatColour != strHatColour)
  52. {
  53. dwarvesList.Add(clsDwarf);
  54. }
  55. else if (dwarfExists.HatColour == strHatColour)
  56. {
  57. if (dwarfExists.Physics < intPhysics)
  58. {
  59. dwarvesList.Remove(dwarfExists);
  60. dwarvesList.Add(clsDwarf);
  61.  
  62. }
  63. }
  64. }
  65. else // doesn't exists
  66. {
  67. dwarvesList.Add(clsDwarf);
  68. }
  69. }
  70. }
  71. }
  72.  
  73. class Dwarf
  74. {
  75. public Dwarf() { }
  76. public Dwarf(string[] strNameHatColourPhysics)
  77. {
  78. Name = strNameHatColourPhysics[0];
  79. HatColour = strNameHatColourPhysics[1];
  80. Physics = int.Parse(strNameHatColourPhysics[2]);
  81. }
  82. public string Name { get; set; }
  83. public string HatColour { get; set; }
  84. public int Physics { get; set; }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement