Advertisement
simonradev

PredicateParty!

Jun 13th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. namespace _10.Predicate_Party_
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             List<string> guests = SplitStringToArray(Console.ReadLine()).ToList();
  12.  
  13.             Dictionary<string, Func<string, string, bool>> allCriteria = new Dictionary<string, Func<string, string, bool>>
  14.             {
  15.                 ["StartsWith"] = (n, s) => n.StartsWith(s),
  16.                 ["EndsWith"] = (n, s) => n.EndsWith(s),
  17.                 ["Length"] = (n, s) => n.Length == int.Parse(s),
  18.             };
  19.  
  20.             Dictionary<string, Action<string, Func<string, string, bool>>> allActions = new Dictionary<string, Action<string, Func<string, string, bool>>>
  21.             {
  22.                 ["Remove"] = (str, func) =>
  23.                 {
  24.                     for (int currentElement = 0; currentElement < guests.Count; currentElement++)
  25.                     {
  26.                         string currentGuest = guests[currentElement];
  27.                         if (func(currentGuest, str))
  28.                         {
  29.                             guests.RemoveAt(currentElement);
  30.                             currentElement--;
  31.                         }
  32.                     }
  33.                 },
  34.                 ["Double"] = (str, func) =>
  35.                 {
  36.                     for (int currentElement = 0; currentElement < guests.Count; currentElement++)
  37.                     {
  38.                         string currentGuest = guests[currentElement];
  39.                         if (func(currentGuest, str))
  40.                         {
  41.                             guests.Insert(currentElement, currentGuest);
  42.                             currentElement++;
  43.                         }
  44.                     }
  45.                 }
  46.             };
  47.  
  48.             string inputLine;
  49.             while ((inputLine = Console.ReadLine()) != "Party!")
  50.             {
  51.                 string[] queryInfo = SplitStringToArray(inputLine);
  52.  
  53.                 string action = queryInfo[0];
  54.                 string criteria = queryInfo[1];
  55.                 string toMatch = queryInfo[2];
  56.  
  57.                 Func<string, string, bool> currentCriteria = allCriteria[criteria];
  58.                 allActions[action](toMatch, currentCriteria);
  59.             }
  60.  
  61.             Console.WriteLine(guests.Count == 0 ? "Nobody is going to the party!" :
  62.                                                  $"{string.Join(", ", guests)} are going to the party!");
  63.         }
  64.  
  65.         private static string[] SplitStringToArray(string toSplit)
  66.         {
  67.             return toSplit.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement