Advertisement
simonradev

PredicateParty!

Apr 13th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. namespace PredicateParty
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public static class Functions
  8.     {
  9.         public static readonly Action<List<string>, string, Func<string, string, bool>> Remove = (l, m, f) =>
  10.                                {
  11.                                    for (int currElement = 0; currElement < l.Count; currElement++)
  12.                                    {
  13.                                        if (f(l[currElement], m))
  14.                                        {
  15.                                            l.RemoveAt(currElement);
  16.                                            currElement--;
  17.                                        }
  18.                                    }
  19.                                };
  20.  
  21.         public static readonly Action<List<string>, string, Func<string, string, bool>> Double = (l, m, f) =>
  22.                                {
  23.                                    for (int currElement = 0; currElement < l.Count; currElement++)
  24.                                    {
  25.                                        if (f(l[currElement], m))
  26.                                        {
  27.                                            l.Insert(currElement, l[currElement]);
  28.                                            currElement++;
  29.                                        }
  30.                                    }
  31.                                };
  32.  
  33.         public static readonly Func<string, string, bool> StartsWith = (f, s)
  34.                                                 => f.Substring(0, s.Length) == s;
  35.  
  36.         public static readonly Func<string, string, bool> EndsWith = (f, s)
  37.                                                 => f.Substring(f.Length - s.Length, s.Length) == s;
  38.  
  39.         public static readonly Func<string, string, bool> HasLenght = (f, s)
  40.                                                 => f.Length == s.Length;
  41.     }
  42.  
  43.     public class PredicateParty
  44.     {
  45.         public static void Main()
  46.         {
  47.             List<string> names = SplitString(Console.ReadLine()).ToList();
  48.  
  49.             string inputLine = Console.ReadLine();
  50.             while (inputLine != "Party!")
  51.             {
  52.                 string[] commandInfo = SplitString(inputLine);
  53.  
  54.                 string guestCommand = commandInfo[0];
  55.                 string command = commandInfo[1];
  56.                 string toMatch = string.Empty;
  57.  
  58.                 if (guestCommand == "Remove")
  59.                 {
  60.                     if (command == "StartsWith")
  61.                     {
  62.                         toMatch = commandInfo[2];
  63.  
  64.                         Functions.Remove(names, toMatch, Functions.StartsWith);
  65.                     }
  66.                     else if (command == "EndsWith")
  67.                     {
  68.                         toMatch = commandInfo[2];
  69.  
  70.                         Functions.Remove(names, toMatch, Functions.EndsWith);
  71.                     }
  72.                     else if (command == "Length")
  73.                     {
  74.                         toMatch = new string(' ', int.Parse(commandInfo[2]));
  75.  
  76.                         Functions.Remove(names, toMatch, Functions.HasLenght);
  77.                     }
  78.                 }
  79.                 else if (guestCommand == "Double")
  80.                 {
  81.                     if (command == "StartsWith")
  82.                     {
  83.                         toMatch = commandInfo[2];
  84.  
  85.                         Functions.Double(names, toMatch, Functions.StartsWith);
  86.                     }
  87.                     else if (command == "EndsWith")
  88.                     {
  89.                         toMatch = commandInfo[2];
  90.  
  91.                         Functions.Double(names, toMatch, Functions.EndsWith);
  92.                     }
  93.                     else if (command == "Length")
  94.                     {
  95.                         toMatch = new string(' ', int.Parse(commandInfo[2]));
  96.  
  97.                         Functions.Double(names, toMatch, Functions.HasLenght);
  98.                     }
  99.                 }
  100.  
  101.                 inputLine = Console.ReadLine();
  102.             }
  103.  
  104.             string result = names.Count == 0 ? "Nobody is" : $"{string.Join(", ", names)} are";
  105.             Console.WriteLine($"{result} going to the party!");
  106.         }
  107.  
  108.         public static string[] SplitString(string toSplit)
  109.         {
  110.             return toSplit.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement