Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _11._The_Party_Reservation_Filter_Module
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> guestList = Console.ReadLine().Split().ToList();
  12.             HashSet<string> actions = new HashSet<string>();
  13.  
  14.             while (true)
  15.             {
  16.                 string command = Console.ReadLine();
  17.                 if (command == "Print")
  18.                 {
  19.                     break;
  20.                 }
  21.  
  22.                 string[] tokens = command.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  23.                 string action = tokens[0];
  24.                 string condition = tokens[1];
  25.                 string filter = tokens[2];
  26.  
  27.                 switch (action)
  28.                 {
  29.                     case "Add filter":
  30.                         actions.Add(new string($"{condition};{filter}"));
  31.                         break;
  32.                     case "Remove filter":
  33.                         actions.Remove(new string($"{condition};{filter}"));
  34.                         break;
  35.                     default:
  36.                         break;
  37.                 }
  38.             }
  39.  
  40.             foreach (var action in actions)
  41.             {
  42.                 string[] tokens = action.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  43.                 string condition = tokens[0];
  44.                 string filter = tokens[1];
  45.  
  46.                 switch (condition)
  47.                 {
  48.                     case "Starts with":
  49.                     case "Ends with":
  50.                         guestList.RemoveAll(x => StartsOrEnds(x, filter, condition)); break;
  51.                     case "Lenght": guestList.RemoveAll(x => x.Length == int.Parse(filter)); break;
  52.                     case "Contains": guestList.RemoveAll(x => x.Contains(filter)); break;
  53.                     default:
  54.                         break;
  55.                 }
  56.             }
  57.  
  58.             Console.WriteLine(string.Join(" ", guestList));
  59.         }
  60.  
  61.         static bool StartsOrEnds(string word, string value, string condition)
  62.         {
  63.             if (condition == "Starts with")
  64.             {
  65.                 for (int i = 0; i < Math.Min(word.Length, value.Length); i++)
  66.                 {
  67.                     if (word[i] != value[i])
  68.                     {
  69.                         return false;
  70.                     }
  71.                 }
  72.                 return true;
  73.             }
  74.             else
  75.             {
  76.                 char[] wordReversed = word.ToCharArray().Reverse().ToArray();
  77.                 char[] valueReversed = value.ToCharArray().Reverse().ToArray();
  78.                 return StartsOrEnds(new string(wordReversed), new string(valueReversed), "Starts with");
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement