Advertisement
elena1234

PredicateParty- how to use Func<>

Jan 27th, 2021
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace PredicateParty_
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var listWithGuests = Console.ReadLine().Split().ToList();
  12.             Func<List<string>, string, string, List<string>> funcRemoveByString = (list, condition, letters) =>
  13.               {
  14.                   var newList = new List<string>();
  15.                   foreach (var person in list)
  16.                   {
  17.                       if (condition == "StartsWith" && !person.StartsWith(letters))
  18.                       {
  19.                           newList.Add(person);
  20.                       }
  21.  
  22.                       else if (condition == "EndsWith" && !person.EndsWith(letters))
  23.                       {
  24.                           newList.Add(person);
  25.                       }
  26.                   }
  27.  
  28.                   return newList;
  29.               };
  30.  
  31.             Func<List<string>, string, string, List<string>> funcDoubleByString = (list, condition, letters) =>
  32.             {
  33.                 var newList = new List<string>();
  34.                 foreach (var person in list)
  35.                 {
  36.                     if (condition == "StartsWith")
  37.                     {
  38.                         newList.Add(person);
  39.                         if (person.StartsWith(letters)) newList.Add(person);
  40.                     }
  41.  
  42.                     else if (condition == "EndsWith")
  43.                     {
  44.                         newList.Add(person);
  45.                         if (person.EndsWith(letters)) newList.Add(person);
  46.                     }
  47.                 }
  48.  
  49.                 return newList;
  50.             };
  51.  
  52.             Func<List<string>, string, int, List<string>> funcOperationByLength = (list, condition, length) =>
  53.              {
  54.                  var newList = new List<string>();
  55.                  foreach (var person in list)
  56.                  {
  57.                      if (condition == "Remove" && person.Length != length)
  58.                      {
  59.                          newList.Add(person);
  60.                      }
  61.  
  62.                      else if (condition == "Double")
  63.                      {
  64.                          newList.Add(person);
  65.                          if (person.Length == length) newList.Add(person);
  66.                      }
  67.                  }
  68.  
  69.                  return newList;
  70.              };
  71.  
  72.             string command;
  73.             while ((command = Console.ReadLine()) != "Party!")
  74.             {
  75.                 if (command.StartsWith("Remove") && !command.Contains("Length"))
  76.                 {
  77.                     string[] commandArray = command.Split();
  78.                     string condition = commandArray[1]; // StartsWith or EndsWith
  79.                     string letters = commandArray[2];
  80.                     listWithGuests = funcRemoveByString(listWithGuests, condition, letters);
  81.                 }
  82.  
  83.                 else if (command.StartsWith("Double") && !command.Contains("Length"))
  84.                 {
  85.                     string[] commandArray = command.Split();
  86.                     string condition = commandArray[1]; // StartsWith or EndsWith
  87.                     string letters = commandArray[2];
  88.                     listWithGuests = funcDoubleByString(listWithGuests, condition, letters);
  89.                 }
  90.  
  91.                 else if (command.Contains("Length"))
  92.                 {
  93.                     string[] commandArray = command.Split();
  94.                     string condition = commandArray[0]; // Double or Remove
  95.                     int length = int.Parse(commandArray[2]);
  96.                     listWithGuests = funcOperationByLength(listWithGuests, condition, length);
  97.                 }
  98.             }
  99.  
  100.             if (listWithGuests.Count > 0)
  101.             {
  102.                 Console.Write(string.Join(", ", listWithGuests));
  103.                 Console.WriteLine(" are going to the party!");
  104.             }
  105.  
  106.             else
  107.             {
  108.                 Console.WriteLine("Nobody is going to the party!");
  109.             }
  110.         }
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement