Advertisement
yanass

PartyReservationFilterModule

Oct 15th, 2019
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace PartyReservationFilterModule
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             List<string> invitations = Console.ReadLine()
  12.                 .Split(new char[] { ' ', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries)
  13.                 .ToList();
  14.  
  15.             List<string> filters = new List<string>();
  16.  
  17.             AddActions(filters);
  18.  
  19.             RemoveFilterBased(invitations, filters);
  20.  
  21.             if (invitations.Count > 0)
  22.                 Console.WriteLine(string.Join(" ", invitations));
  23.         }
  24.  
  25.         static void AddActions(List<string> filters)
  26.         {
  27.             while (true)
  28.             {
  29.                 string[] command = Console.ReadLine().Split(";");
  30.                 if (command[0] == "Print")
  31.                 {
  32.                     break;
  33.                 }
  34.  
  35.                 switch (command[0])
  36.                 {
  37.                     case "Add filter":
  38.                         filters.Add(CheckConditions(command));
  39.                         break;
  40.                     case "Remove filter":
  41.                         filters.Remove(CheckConditions(command));
  42.                         break;
  43.                 }
  44.             }
  45.         }
  46.         static string CheckConditions(string[] command)
  47.         {
  48.             string condition = string.Empty;
  49.             switch (command[1])
  50.             {
  51.                 case "Starts with":
  52.                     condition = "Starts " + command[2];
  53.                     break;
  54.                 case "Ends with":
  55.                     condition = "Ends " + command[2];
  56.                     break;
  57.                 case "Length":
  58.                     condition = "Length " + command[2];
  59.                     break;
  60.                 case "Contains":
  61.                     condition = "Contains " + command[2];
  62.                     break;
  63.             }
  64.  
  65.             return condition;
  66.         }
  67.  
  68.         static void RemoveFilterBased(List<string> invitations, List<string> filters)
  69.         {
  70.             Func<string, string, bool> startsWith = (a, b) => a.StartsWith(b);
  71.             Func<string, string, bool> endsWith = (a, b) => a.EndsWith(b);
  72.             Func<string, string, bool> contains = (a, b) => a.Contains(b);
  73.             Func<string, int, bool> lengthFilter = (a, b) => a.Length >= b;
  74.  
  75.             while (filters.Count > 0)
  76.             {
  77.                 string[] filter = filters.Last().Split();
  78.                 filters.RemoveAt(filters.Count - 1);
  79.                 switch (filter[0])
  80.                 {
  81.                     case "Starts":
  82.                         invitations.RemoveAll(i => startsWith(i, filter[1]));
  83.                         break;
  84.                     case "Ends":
  85.                         invitations.RemoveAll(i => endsWith(i, filter[1]));
  86.                         break;
  87.                     case "Length":
  88.                         invitations.RemoveAll(i => lengthFilter(i, int.Parse(filter[1])));
  89.                         break;
  90.                     case "Contains":
  91.                         invitations.RemoveAll(i => contains(i, filter[1]));
  92.                         break;
  93.                 }
  94.             }
  95.  
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement