Advertisement
GabrielDas

Untitled

Jun 4th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace P10PredicateParty
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var people = Console.ReadLine()
  12. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  13. .ToList();
  14.  
  15. var filters = new List<string>();
  16.  
  17. Func<string, int, bool> lengthFilter = (name, length) => name.Length == length;
  18. Func<string, string, bool> startsWithFilter = (name, param) => name.StartsWith(param);
  19. Func<string, string, bool> endsWithFilter = (name, param) => name.EndsWith(param);
  20.  
  21. while (true)
  22. {
  23. string[] commands = Console.ReadLine()
  24. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  25. .ToArray();
  26.  
  27. if (commands[0] == "Party!")
  28. {
  29. break;
  30. }
  31. else
  32. {
  33. filters.Add($"{commands[0]} {commands[1]} {commands[2]}");
  34.  
  35. }
  36.  
  37. }
  38.  
  39. foreach (var item in filters)
  40. {
  41. string[] comms = item.Split();
  42.  
  43. string action = comms[0];
  44. string condition = comms[1];
  45. string parameter = comms[2];
  46.  
  47. if (action == "Remove")
  48. {
  49. if (condition == "StartsWith")
  50. {
  51. people = people.Where(name => !startsWithFilter(name, parameter)).ToList();
  52.  
  53. }
  54. else if (condition == "EndsWith")
  55. {
  56. people = people.Where(name => !endsWithFilter(name, parameter)).ToList();
  57. }
  58. else
  59. {
  60. int tempPar = int.Parse(parameter);
  61. people = people.Where(name => !lengthFilter(name, tempPar)).ToList();
  62.  
  63. }
  64.  
  65. }
  66. else if (action == "Double")
  67. {
  68. if (condition == "StartsWith")
  69. {
  70. string tempName = String.Empty;
  71.  
  72. var tempList =
  73. people.Where(name => startsWithFilter(name, parameter)).ToList();
  74.  
  75. if (tempList.Count > 0)
  76. {
  77. foreach (var i in tempList)
  78. {
  79. tempName = i;
  80. break;
  81. }
  82. }
  83.  
  84. int index = people.IndexOf(tempName);
  85.  
  86. if (index < people.Count - 1)
  87. {
  88. for (int i = 0; i < tempList.Count; i++)
  89. {
  90. people.Insert(index + 1, tempName);
  91.  
  92. }
  93.  
  94. }
  95. else
  96. {
  97. people.Add(tempName);
  98. }
  99.  
  100. }
  101. else if (condition == "EndsWith")
  102. {
  103. string tempName = String.Empty;
  104.  
  105. var tempList =
  106. people.Where(name => endsWithFilter(name, parameter)).ToList();
  107.  
  108. if (tempList.Count > 0)
  109. {
  110. foreach (var i in tempList)
  111. {
  112. tempName = i;
  113. break;
  114. }
  115. }
  116.  
  117. int index = people.IndexOf(tempName);
  118.  
  119. if (index < people.Count - 1)
  120. {
  121. for (int i = 0; i < tempList.Count; i++)
  122. {
  123. people.Insert(index + 1, tempName);
  124.  
  125. }
  126. }
  127. else
  128. {
  129. people.Add(tempName);
  130. }
  131.  
  132. }
  133. else
  134. {
  135. int tempPar = int.Parse(parameter);
  136. string tempName = String.Empty;
  137.  
  138. var tempList =
  139. people.Where(name => lengthFilter(name, tempPar)).ToList();
  140.  
  141. if (tempList.Count > 0)
  142. {
  143. foreach (var i in tempList)
  144. {
  145. tempName = i;
  146. break;
  147. }
  148. }
  149.  
  150. int index = people.IndexOf(tempName);
  151.  
  152. if (index < people.Count - 1)
  153. {
  154. for (int i = 0; i < tempList.Count; i++)
  155. {
  156. people.Insert(index + 1, tempName);
  157.  
  158. }
  159. }
  160. else
  161. {
  162. people.Add(tempName);
  163. }
  164.  
  165. }
  166. }
  167.  
  168. }
  169. if (people.Count > 0)
  170. {
  171. Console.WriteLine($"{String.Join(", ", people)} are going to the party!");
  172. }
  173. else
  174. {
  175. Console.WriteLine("Nobody is going to the party!");
  176. }
  177. }
  178.  
  179.  
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement