MiroslavKisov

PartyReservationFilterModule

Feb 7th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace PartyReservationFilterModule
  6. {
  7.     public class StartUp
  8.     {
  9.         public static void Main()
  10.         {
  11.             var names = Console.ReadLine()
  12.                 .Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
  13.                 .ToArray();
  14.             var indexes = new Dictionary<string, Dictionary<int, string>>();
  15.             string command;
  16.             while ((command = Console.ReadLine()) != "Print")
  17.             {
  18.                 var commandArgs = command.Split(';').ToArray();
  19.  
  20.                 if (commandArgs[0] == "Add filter")
  21.                 {
  22.                     if (commandArgs[1] == "Starts with")
  23.                     {
  24.                         AddFilterStartsWith(names, indexes, commandArgs);
  25.                     }
  26.                     else if (commandArgs[1] == "Ends with")
  27.                     {
  28.                         AddFilterEndsWith(names, indexes, commandArgs);
  29.                     }
  30.                     else if (commandArgs[1] == "Length")
  31.                     {
  32.                         AddFilterLength(names, indexes, commandArgs);
  33.                     }
  34.                     else if (commandArgs[1] == "Contains")
  35.                     {
  36.                         AddFilterContains(names, indexes, commandArgs);
  37.                     }
  38.                 }
  39.                 else if (commandArgs[0] == "Remove filter")
  40.                 {
  41.                     if (commandArgs[1] == "Starts with")
  42.                     {
  43.                         RemoveFilter(names, indexes, commandArgs);
  44.                     }
  45.                     else if (commandArgs[1] == "Ends with")
  46.                     {
  47.                         RemoveFilter(names, indexes, commandArgs);
  48.                     }
  49.                     else if (commandArgs[1] == "Length")
  50.                     {
  51.                         RemoveFilter(names, indexes, commandArgs);
  52.                     }
  53.                     else if (commandArgs[1] == "Contains")
  54.                     {
  55.                         RemoveFilter(names, indexes, commandArgs);
  56.                     }
  57.                 }
  58.             }
  59.             Console.WriteLine(string.Join(" ", names.Where(n => n != " ")));
  60.         }
  61.  
  62.         private static void AddFilterContains(string[] names, Dictionary<string, Dictionary<int, string>> indexes, string[] commandArgs)
  63.         {
  64.             for (int i = 0; i < names.Length; i++)
  65.             {
  66.                 if (names[i].Contains(commandArgs[2]))
  67.                 {
  68.                     if (!indexes.ContainsKey(commandArgs[2]))
  69.                     {
  70.                         indexes.Add(commandArgs[2], new Dictionary<int, string>());
  71.                     }
  72.                     indexes[commandArgs[2]].Add(i, names[i]);
  73.                     names[i] = " ";
  74.                 }
  75.             }
  76.         }
  77.  
  78.         private static void AddFilterLength(string[] names, Dictionary<string, Dictionary<int, string>> indexes, string[] commandArgs)
  79.         {
  80.             for (int i = 0; i < names.Length; i++)
  81.             {
  82.                 if (names[i].Length == int.Parse(commandArgs[2]))
  83.                 {
  84.                     if (!indexes.ContainsKey(commandArgs[2]))
  85.                     {
  86.                         indexes.Add(commandArgs[2], new Dictionary<int, string>());
  87.                     }
  88.                     indexes[commandArgs[2]].Add(i, names[i]);
  89.                     names[i] = " ";
  90.                 }
  91.             }
  92.         }
  93.  
  94.         private static void AddFilterEndsWith(string[] names, Dictionary<string, Dictionary<int, string>> indexes, string[] commandArgs)
  95.         {
  96.             for (int i = 0; i < names.Length; i++)
  97.             {
  98.                 if (names[i].EndsWith(commandArgs[2]))
  99.                 {
  100.                     if (!indexes.ContainsKey(commandArgs[2]))
  101.                     {
  102.                         indexes.Add(commandArgs[2], new Dictionary<int, string>());
  103.                     }
  104.                     indexes[commandArgs[2]].Add(i, names[i]);
  105.                     names[i] = " ";
  106.                 }
  107.             }
  108.         }
  109.  
  110.         private static void AddFilterStartsWith(string[] names, Dictionary<string, Dictionary<int, string>> indexes, string[] commandArgs)
  111.         {
  112.             for (int i = 0; i < names.Length; i++)
  113.             {
  114.                 if (names[i].StartsWith(commandArgs[2]))
  115.                 {
  116.                     if (!indexes.ContainsKey(commandArgs[2]))
  117.                     {
  118.                         indexes.Add(commandArgs[2], new Dictionary<int, string>());
  119.                     }
  120.                     indexes[commandArgs[2]].Add(i, names[i]);
  121.                     names[i] = " ";
  122.                 }
  123.             }
  124.         }
  125.  
  126.         private static void RemoveFilter(string[] names, Dictionary<string, Dictionary<int, string>> indexes, string[] commandArgs)
  127.         {
  128.             for (int i = 0; i < names.Length; i++)
  129.             {
  130.                 if (names[i] == " " && indexes[commandArgs[2]].ContainsKey(i))
  131.                 {
  132.                     names[i] = indexes[commandArgs[2]][i];
  133.                 }
  134.             }
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment