Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _10.PredicateParty
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<string> names = Console.ReadLine().Split().ToList();
- string command = Console.ReadLine();
- Func<string, string, bool> predicate = (name, isContains) => name.EndsWith(isContains);
- Queue<string> data = new Queue<string>();
- while(command != "Party!")
- {
- string[] line = command.Split();
- if(line[0] == "Remove")
- {
- string cmd = line[1];
- if (cmd == "StartsWith")
- {
- string word = line[2];
- predicate = (name, isContains) => name.StartsWith(isContains);
- foreach (string item in names)
- {
- if(predicate(item, word))
- {
- data.Enqueue(item);
- }
- }
- for (int i = 0; i <= data.Count; i++)
- {
- names.Remove(data.Dequeue());
- }
- }
- else if (cmd == "EndsWith")
- {
- predicate = (name, isContains) => name.EndsWith(isContains);
- string word = line[2];
- foreach (var item in names)
- {
- if(predicate(item, word))
- {
- names.Remove(item);
- }
- }
- }
- }
- else if(line[0] == "Double")
- {
- string cmd = line[1];
- if(cmd == "StartsWith")
- {
- string word = line[2];
- predicate = (name, isContains) => name.StartsWith(isContains);
- for (int i = 0; i < names.Count; i++)
- {
- if(predicate(names[i], word))
- {
- data.Enqueue(names[i]);
- }
- }
- for (int i = 0; i < data.Count; i++)
- {
- names.Add(data.Dequeue());
- }
- }
- else if(cmd == "EndsWith")
- {
- string word = line[2];
- predicate = (name, isContains) => name.EndsWith(isContains);
- for (int i = 0; i < names.Count; i++)
- {
- if (predicate(names[i], word))
- {
- data.Enqueue(names[i]);
- }
- }
- for (int i = 0; i <= data.Count; i++)
- {
- names.Add(data.Dequeue());
- }
- }
- else if(line[1] == "Length")
- {
- int length = int.Parse(line[2]);
- Func<int, int, bool> myPredicate = (wordLength, cmdLength) => wordLength == length;
- for (int i = 0; i < names.Count; i++)
- {
- int wordLength = names[i].Length;
- if(myPredicate(wordLength, length))
- {
- data.Enqueue(names[i]);
- }
- }
- for (int i = 0; i <= data.Count; i++)
- {
- names.Add(data.Dequeue());
- }
- }
- }
- command = Console.ReadLine();
- }
- if(names.Count != 0)
- {
- Console.WriteLine($"{string.Join(", ", names.OrderBy(s => s))} are going to the party!");
- }
- else
- {
- Console.WriteLine("Nobody is going to the party!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment