yanass

Predicate Party! - Functional Programming

Oct 14th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 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()
  10.         {
  11.             List<string> namesParty = Console.ReadLine()
  12.                 .Split()
  13.                 .ToList();
  14.  
  15.             CheckRequirements(namesParty);
  16.             PrintComingToParty(namesParty);
  17.  
  18.         }
  19.  
  20.         static void CheckRequirements(List<string> partyPeople)
  21.         {
  22.             while (true)
  23.             {
  24.                 string[] command = Console.ReadLine()
  25.                     .Split(new char[] { ' ', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  26.  
  27.                 if (command[0] == "Party!")
  28.                 {
  29.                     break;
  30.                 }
  31.  
  32.                 switch (command[1])
  33.                 {
  34.                     case "StartsWith":  //certain string
  35.                         TakeActions(command[0], partyPeople, n => n.StartsWith(command[2]));
  36.                         break;
  37.                     case "EndsWith":
  38.                         TakeActions(command[0], partyPeople, n => n.EndsWith(command[2]));
  39.                         break;
  40.                     case "Length": //check name length
  41.                         TakeActions(command[0], partyPeople, l => l.Length == int.Parse(command[2]));
  42.                         break;
  43.                     default:
  44.                         break;
  45.                 }
  46.             }
  47.         }
  48.         static void TakeActions(string command, List<string> partyPeople, Func<string, bool> condition)
  49.         {
  50.             //if counting from 0, gives Memory Limit problem in Judge
  51.             for (int i = partyPeople.Count - 1; i >= 0; i--)
  52.             {
  53.                 if (condition(partyPeople[i]))
  54.                 {
  55.                     switch (command)
  56.                     {
  57.                         case "Remove":
  58.                             partyPeople.RemoveAt(i);
  59.                             break;
  60.  
  61.                         case "Double":
  62.                             partyPeople.Add(partyPeople[i]);
  63.                             break;
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.         static void PrintComingToParty(List<string> partyPeople)
  69.         {
  70.             if(partyPeople.Any())
  71.             {
  72.                 Console.WriteLine(string.Join(", ", partyPeople) + " are going to the party!");
  73.             }
  74.  
  75.             else
  76.             {
  77.                 Console.WriteLine("Nobody is going to the party!");
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment