Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace PredicateParty
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public static class Functions
- {
- public static readonly Action<List<string>, string, Func<string, string, bool>> Remove = (l, m, f) =>
- {
- for (int currElement = 0; currElement < l.Count; currElement++)
- {
- if (f(l[currElement], m))
- {
- l.RemoveAt(currElement);
- currElement--;
- }
- }
- };
- public static readonly Action<List<string>, string, Func<string, string, bool>> Double = (l, m, f) =>
- {
- for (int currElement = 0; currElement < l.Count; currElement++)
- {
- if (f(l[currElement], m))
- {
- l.Insert(currElement, l[currElement]);
- currElement++;
- }
- }
- };
- public static readonly Func<string, string, bool> StartsWith = (f, s)
- => f.Substring(0, s.Length) == s;
- public static readonly Func<string, string, bool> EndsWith = (f, s)
- => f.Substring(f.Length - s.Length, s.Length) == s;
- public static readonly Func<string, string, bool> HasLenght = (f, s)
- => f.Length == s.Length;
- }
- public class PredicateParty
- {
- public static void Main()
- {
- List<string> names = SplitString(Console.ReadLine()).ToList();
- string inputLine = Console.ReadLine();
- while (inputLine != "Party!")
- {
- string[] commandInfo = SplitString(inputLine);
- string guestCommand = commandInfo[0];
- string command = commandInfo[1];
- string toMatch = string.Empty;
- if (guestCommand == "Remove")
- {
- if (command == "StartsWith")
- {
- toMatch = commandInfo[2];
- Functions.Remove(names, toMatch, Functions.StartsWith);
- }
- else if (command == "EndsWith")
- {
- toMatch = commandInfo[2];
- Functions.Remove(names, toMatch, Functions.EndsWith);
- }
- else if (command == "Length")
- {
- toMatch = new string(' ', int.Parse(commandInfo[2]));
- Functions.Remove(names, toMatch, Functions.HasLenght);
- }
- }
- else if (guestCommand == "Double")
- {
- if (command == "StartsWith")
- {
- toMatch = commandInfo[2];
- Functions.Double(names, toMatch, Functions.StartsWith);
- }
- else if (command == "EndsWith")
- {
- toMatch = commandInfo[2];
- Functions.Double(names, toMatch, Functions.EndsWith);
- }
- else if (command == "Length")
- {
- toMatch = new string(' ', int.Parse(commandInfo[2]));
- Functions.Double(names, toMatch, Functions.HasLenght);
- }
- }
- inputLine = Console.ReadLine();
- }
- string result = names.Count == 0 ? "Nobody is" : $"{string.Join(", ", names)} are";
- Console.WriteLine($"{result} going to the party!");
- }
- public static string[] SplitString(string toSplit)
- {
- return toSplit.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement