osman1997

10. Predicate party

Jan 31st, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _10.PredicateParty
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<string> names = Console.ReadLine().Split().ToList();
  12.  
  13. string command = Console.ReadLine();
  14.  
  15. Func<string, string, bool> predicate = (name, isContains) => name.EndsWith(isContains);
  16.  
  17. Queue<string> data = new Queue<string>();
  18.  
  19.  
  20.  
  21. while(command != "Party!")
  22. {
  23. string[] line = command.Split();
  24.  
  25. if(line[0] == "Remove")
  26. {
  27. string cmd = line[1];
  28.  
  29. if (cmd == "StartsWith")
  30. {
  31. string word = line[2];
  32. predicate = (name, isContains) => name.StartsWith(isContains);
  33.  
  34. foreach (string item in names)
  35. {
  36. if(predicate(item, word))
  37. {
  38. data.Enqueue(item);
  39. }
  40. }
  41. for (int i = 0; i <= data.Count; i++)
  42. {
  43. names.Remove(data.Dequeue());
  44. }
  45.  
  46. }
  47. else if (cmd == "EndsWith")
  48. {
  49. predicate = (name, isContains) => name.EndsWith(isContains);
  50.  
  51. string word = line[2];
  52. foreach (var item in names)
  53. {
  54. if(predicate(item, word))
  55. {
  56. names.Remove(item);
  57. }
  58. }
  59. }
  60. }
  61. else if(line[0] == "Double")
  62. {
  63. string cmd = line[1];
  64.  
  65. if(cmd == "StartsWith")
  66. {
  67.  
  68. string word = line[2];
  69. predicate = (name, isContains) => name.StartsWith(isContains);
  70.  
  71. for (int i = 0; i < names.Count; i++)
  72. {
  73. if(predicate(names[i], word))
  74. {
  75. data.Enqueue(names[i]);
  76. }
  77. }
  78. for (int i = 0; i < data.Count; i++)
  79. {
  80. names.Add(data.Dequeue());
  81. }
  82. }
  83. else if(cmd == "EndsWith")
  84. {
  85. string word = line[2];
  86. predicate = (name, isContains) => name.EndsWith(isContains);
  87.  
  88. for (int i = 0; i < names.Count; i++)
  89. {
  90. if (predicate(names[i], word))
  91. {
  92. data.Enqueue(names[i]);
  93. }
  94. }
  95. for (int i = 0; i <= data.Count; i++)
  96. {
  97. names.Add(data.Dequeue());
  98. }
  99. }
  100. else if(line[1] == "Length")
  101. {
  102. int length = int.Parse(line[2]);
  103.  
  104. Func<int, int, bool> myPredicate = (wordLength, cmdLength) => wordLength == length;
  105.  
  106. for (int i = 0; i < names.Count; i++)
  107. {
  108. int wordLength = names[i].Length;
  109.  
  110. if(myPredicate(wordLength, length))
  111. {
  112. data.Enqueue(names[i]);
  113. }
  114. }
  115.  
  116. for (int i = 0; i <= data.Count; i++)
  117. {
  118. names.Add(data.Dequeue());
  119. }
  120. }
  121. }
  122.  
  123. command = Console.ReadLine();
  124. }
  125.  
  126. if(names.Count != 0)
  127. {
  128. Console.WriteLine($"{string.Join(", ", names.OrderBy(s => s))} are going to the party!");
  129. }
  130. else
  131. {
  132. Console.WriteLine("Nobody is going to the party!");
  133. }
  134. }
  135. }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment