Advertisement
softy_02

PredicateParty!

Jun 17th, 2020
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace PredicateParty_
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var guests = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList();
  12.  
  13. while (true)
  14. {
  15. var line = Console.ReadLine();
  16. if (line == "Party!")
  17. {
  18. break;
  19. }
  20.  
  21. var command = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
  22.  
  23. var cmdType = command[0];
  24. var predicateArgs = command.Skip(1).ToArray();
  25.  
  26. Predicate<string> predicate = GetPredicate(predicateArgs);
  27.  
  28. if (cmdType == "Remove")
  29. {
  30. guests.RemoveAll(predicate);
  31. }
  32. else if (cmdType == "Double")
  33. {
  34. DoubleGuests(guests, predicate);
  35. }
  36.  
  37. }
  38.  
  39. if (guests.Any())
  40. {
  41. Console.WriteLine($"{string.Join(", ", guests)} are going to the party!");
  42. }
  43. else
  44. {
  45. Console.WriteLine("Nobody is going to the party!");
  46. }
  47. }
  48.  
  49. private static void DoubleGuests(List<string> guests, Predicate<string> predicate)
  50. {
  51. for (int i = 0; i < guests.Count; i++)
  52. {
  53. var currGuest = guests[i];
  54. if (predicate(currGuest))
  55. {
  56. guests.Insert(i + 1, currGuest);
  57. i++;
  58. }
  59. }
  60. }
  61.  
  62. static Predicate<string> GetPredicate(string[] predicateArgs)
  63. {
  64. var type = predicateArgs[0];
  65. var argument = predicateArgs[1];
  66.  
  67. Predicate<string> predicate = null;
  68.  
  69. if (type == "StartsWith")
  70. {
  71. predicate = new Predicate<string>(name =>
  72. {
  73. return name.StartsWith(argument);
  74. });
  75. }
  76. else if (type == "EndsWith")
  77. {
  78. predicate = new Predicate<string>(name =>
  79. {
  80. return name.EndsWith(argument);
  81. });
  82. }
  83. else if (type == "Length")
  84. {
  85. predicate = new Predicate<string>(name =>
  86. {
  87. return name.Length == int.Parse(argument);
  88. });
  89. }
  90.  
  91. return predicate;
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement