Advertisement
desislava_topuzakova

09. Predicate Party!

Jun 3rd, 2022
1,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _9._Predicate_Party_
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> people = Console.ReadLine().Split().ToList();
  12.             //"Peter Misha Stephen".Split() -> {"Peter", "Misha", "Stephen"}
  13.  
  14.             string command = Console.ReadLine();
  15.  
  16.             while (command != "Party!")
  17.             {
  18.                 //приема име -> връща true/ false ако името отговаря на даден критерий
  19.                 Predicate<string> predicate = GetPredicate(command);
  20.  
  21.                 //command = "Double ....." или "Remove ....."
  22.                 if (command.StartsWith("Double"))
  23.                 {
  24.                     //дублираме всички, които отговарят на даден критерий
  25.                     //StartsWith, EndsWith, Length
  26.  
  27.                     //{Peter, Ivan, Dragan} -> {Peter, Peter, Ivan, Dragan}
  28.                     for (int i = 0; i < people.Count; i++)
  29.                     {
  30.                         string person = people[i];
  31.                         if (predicate(person))
  32.                         {
  33.                             people.Insert(i + 1, person);
  34.                             i++;
  35.                         }
  36.                     }
  37.                 }
  38.                 else if (command.StartsWith("Remove"))
  39.                 {
  40.                     //премахваме всички, които отговарят на даден критерий
  41.                     //StartsWith, EndsWith, Length
  42.                     people.RemoveAll(predicate);
  43.  
  44.                 }
  45.  
  46.  
  47.                 command = Console.ReadLine();
  48.             }
  49.  
  50.             //отпечатай гостите
  51.             if (people.Count == 0)
  52.             {
  53.                 Console.WriteLine("Nobody is going to the party!");
  54.             }
  55.             else
  56.             {
  57.                 Console.WriteLine($"{string.Join(", ", people)} are going to the party!");
  58.             }
  59.         }
  60.  
  61.         private static Predicate<string> GetPredicate(string command)
  62.         {
  63.             //"Double StartsWith a".Split() -> ["Double", "StartsWith", "a"]
  64.             string command2 = command.Split()[1]; ////StartsWith, EndsWith, Length
  65.             string arg = command.Split()[2];
  66.  
  67.             Predicate<string> predicate = null;
  68.             if (command2 == "StartsWith")
  69.             {
  70.                 predicate = name => name.StartsWith(arg);
  71.             }
  72.             else if (command2 == "EndsWith")
  73.             {
  74.                 predicate = name => name.EndsWith(arg);
  75.             }
  76.             else if (command2 == "Length")
  77.             {
  78.                 //"Remove Length 3".Split() -> ["Remove", "Length", "3"]
  79.                 predicate = name => name.Length == int.Parse(arg);
  80.             }
  81.             return predicate;
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement