Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _10.Predicate_Party_
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class Program
- {
- public static void Main()
- {
- List<string> guests = SplitStringToArray(Console.ReadLine()).ToList();
- Dictionary<string, Func<string, string, bool>> allCriteria = new Dictionary<string, Func<string, string, bool>>
- {
- ["StartsWith"] = (n, s) => n.StartsWith(s),
- ["EndsWith"] = (n, s) => n.EndsWith(s),
- ["Length"] = (n, s) => n.Length == int.Parse(s),
- };
- Dictionary<string, Action<string, Func<string, string, bool>>> allActions = new Dictionary<string, Action<string, Func<string, string, bool>>>
- {
- ["Remove"] = (str, func) =>
- {
- for (int currentElement = 0; currentElement < guests.Count; currentElement++)
- {
- string currentGuest = guests[currentElement];
- if (func(currentGuest, str))
- {
- guests.RemoveAt(currentElement);
- currentElement--;
- }
- }
- },
- ["Double"] = (str, func) =>
- {
- for (int currentElement = 0; currentElement < guests.Count; currentElement++)
- {
- string currentGuest = guests[currentElement];
- if (func(currentGuest, str))
- {
- guests.Insert(currentElement, currentGuest);
- currentElement++;
- }
- }
- }
- };
- string inputLine;
- while ((inputLine = Console.ReadLine()) != "Party!")
- {
- string[] queryInfo = SplitStringToArray(inputLine);
- string action = queryInfo[0];
- string criteria = queryInfo[1];
- string toMatch = queryInfo[2];
- Func<string, string, bool> currentCriteria = allCriteria[criteria];
- allActions[action](toMatch, currentCriteria);
- }
- Console.WriteLine(guests.Count == 0 ? "Nobody is going to the party!" :
- $"{string.Join(", ", guests)} are going to the party!");
- }
- private static string[] SplitStringToArray(string toSplit)
- {
- return toSplit.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement