Advertisement
gabi11

Functional Programming - 10. Predicate Party!

May 29th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Advanced
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var guests = Console.ReadLine().Split().ToList();
  14.  
  15.             Func<string, string, string, bool> filter = (guest, action, criteria) => action == "EndsWith" ?
  16.                                                             guest.EndsWith(criteria) : action == "StartsWith" ?
  17.                                                             guest.StartsWith(criteria) : guest.Length == int.Parse(criteria);
  18.  
  19.  
  20.             while (true)
  21.             {
  22.                 var input = Console.ReadLine().Split();
  23.  
  24.                 if (input[0] == "Party!")
  25.                 {
  26.                     break;
  27.                 }
  28.  
  29.                 var command = input[0];
  30.                 var action = input[1];
  31.                 var criteria = input[2];
  32.  
  33.                 if (command == "Remove")
  34.                 {
  35.                     guests = guests.Where(g => !filter(g, action, criteria)).ToList();
  36.                 }
  37.  
  38.                 else if (command == "Double")
  39.                 {
  40.                     var duplicate = guests.Where(g => filter(g, action, criteria)).ToList();
  41.  
  42.                     foreach (var guest in duplicate)
  43.                     {
  44.                         int index = guests.IndexOf(guest) + 1;
  45.  
  46.                         guests.Insert(index, guest);
  47.                     }
  48.                 }
  49.             }
  50.  
  51.             if (guests.Any())
  52.             {
  53.                 Console.WriteLine($"{string.Join(", ", guests)} are going to the party!");
  54.             }
  55.             else
  56.             {
  57.                 Console.WriteLine("Nobody is going to the party!");
  58.             }
  59.         }
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement