IPetrov007

LINQustics

Mar 16th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03_LINQuistics
  6. {
  7. class LINQuistics
  8. {
  9. static void Main(string[] args)
  10. {
  11. var result = new Dictionary<string, List<string>>();
  12. var inputLine = Console.ReadLine();
  13. var collection = String.Empty;
  14.  
  15. while (!inputLine.Equals("exit"))
  16. {
  17. var input = inputLine
  18. .Split(new[] { '.', '(', ')', ' ' }, StringSplitOptions.RemoveEmptyEntries)
  19. .ToArray();
  20.  
  21. collection = input[0];
  22. int count = 0;
  23.  
  24. if (input.Count() > 1)
  25. {
  26. AddToCollection(result, collection, input);
  27. }
  28. else if (result.ContainsKey(collection))
  29. {
  30. PrintCollectionsElements(result, collection);
  31. }
  32. else if (int.TryParse(input[0], out count) && result.Count > 0)
  33. {
  34. PrintNMethods(result, count);
  35. }
  36.  
  37. inputLine = Console.ReadLine();
  38. }
  39.  
  40.  
  41. var finalInput = Console.ReadLine().Split(' ').ToArray();
  42. var method = finalInput[0];
  43.  
  44. Dictionary<string, List<string>> finalResult = FinalResultOrder(result, method);
  45.  
  46. var selection = finalInput[1];
  47.  
  48. if (selection.Equals("all"))
  49. {
  50. PrintAll(finalResult);
  51. }
  52. else if (selection.Equals("collection"))
  53. {
  54. PrintOnlyCollections(finalResult);
  55. }
  56. }
  57.  
  58. private static void AddToCollection(Dictionary<string, List<string>> result, string collection, string[] input)
  59. {
  60. if (!result.ContainsKey(collection))
  61. {
  62. result[collection] = new List<string>();
  63. }
  64. for (int i = 1; i < input.Length; i++)
  65. {
  66. if (!result[collection].Contains(input[i]))
  67. {
  68. result[collection].Add(input[i]);
  69. }
  70. }
  71. }
  72.  
  73. private static Dictionary<string, List<string>> FinalResultOrder(Dictionary<string, List<string>> result, string method)
  74. {
  75. return result
  76. .Where(y => y.Value.Contains(method))
  77. .OrderByDescending(y => y.Value.Count)
  78. .ThenByDescending(y => y.Value.Min(z => z.Length))
  79. .ToDictionary(x => x.Key, x => x.Value);
  80. }
  81.  
  82. private static void PrintOnlyCollections(Dictionary<string, List<string>> finalResult)
  83. {
  84. foreach (var key in finalResult.Keys)
  85. {
  86. Console.WriteLine(key);
  87. }
  88. }
  89.  
  90. private static void PrintAll(Dictionary<string, List<string>> finalResult)
  91. {
  92. foreach (var key in finalResult)
  93. {
  94. Console.WriteLine(key.Key);
  95.  
  96. foreach (var item in finalResult[key.Key].OrderByDescending(x => x.Count()))
  97. {
  98. Console.WriteLine("* {0}", item);
  99. }
  100. }
  101. }
  102.  
  103. private static void PrintNMethods(Dictionary<string, List<string>> result, int count)
  104. {
  105. var printNMethods = result.Values
  106. .OrderByDescending(x => x.Count).First()
  107. .OrderBy(x => x.Length)
  108. .Take(count)
  109. .ToList();
  110.  
  111. foreach (var item in printNMethods)
  112. {
  113. Console.WriteLine("* {0}", item);
  114. }
  115. }
  116.  
  117. private static void PrintCollectionsElements(Dictionary<string, List<string>> result, string collection)
  118. {
  119. var resultForPrinting = result[collection]
  120. .OrderByDescending(x => x.Length)
  121. .ThenByDescending(x => x.Distinct().Count())
  122. .ToList();
  123. foreach (var values in resultForPrinting)
  124. {
  125. Console.WriteLine("* {0}", values);
  126. }
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment