Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace P10PredicateParty
- {
- class Program
- {
- static void Main(string[] args)
- {
- var people = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .ToList();
- var filters = new List<string>();
- Func<string, int, bool> lengthFilter = (name, length) => name.Length == length;
- Func<string, string, bool> startsWithFilter = (name, param) => name.StartsWith(param);
- Func<string, string, bool> endsWithFilter = (name, param) => name.EndsWith(param);
- while (true)
- {
- string[] commands = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .ToArray();
- if (commands[0] == "Party!")
- {
- break;
- }
- else
- {
- filters.Add($"{commands[0]} {commands[1]} {commands[2]}");
- }
- }
- foreach (var item in filters)
- {
- string[] comms = item.Split();
- string action = comms[0];
- string condition = comms[1];
- string parameter = comms[2];
- if (action == "Remove")
- {
- if (condition == "StartsWith")
- {
- people = people.Where(name => !startsWithFilter(name, parameter)).ToList();
- }
- else if (condition == "EndsWith")
- {
- people = people.Where(name => !endsWithFilter(name, parameter)).ToList();
- }
- else
- {
- int tempPar = int.Parse(parameter);
- people = people.Where(name => !lengthFilter(name, tempPar)).ToList();
- }
- }
- else if (action == "Double")
- {
- if (condition == "StartsWith")
- {
- string tempName = String.Empty;
- var tempList =
- people.Where(name => startsWithFilter(name, parameter)).ToList();
- if (tempList.Count > 0)
- {
- foreach (var i in tempList)
- {
- tempName = i;
- break;
- }
- }
- int index = people.IndexOf(tempName);
- if (index < people.Count - 1)
- {
- for (int i = 0; i < tempList.Count; i++)
- {
- people.Insert(index + 1, tempName);
- }
- }
- else
- {
- people.Add(tempName);
- }
- }
- else if (condition == "EndsWith")
- {
- string tempName = String.Empty;
- var tempList =
- people.Where(name => endsWithFilter(name, parameter)).ToList();
- if (tempList.Count > 0)
- {
- foreach (var i in tempList)
- {
- tempName = i;
- break;
- }
- }
- int index = people.IndexOf(tempName);
- if (index < people.Count - 1)
- {
- for (int i = 0; i < tempList.Count; i++)
- {
- people.Insert(index + 1, tempName);
- }
- }
- else
- {
- people.Add(tempName);
- }
- }
- else
- {
- int tempPar = int.Parse(parameter);
- string tempName = String.Empty;
- var tempList =
- people.Where(name => lengthFilter(name, tempPar)).ToList();
- if (tempList.Count > 0)
- {
- foreach (var i in tempList)
- {
- tempName = i;
- break;
- }
- }
- int index = people.IndexOf(tempName);
- if (index < people.Count - 1)
- {
- for (int i = 0; i < tempList.Count; i++)
- {
- people.Insert(index + 1, tempName);
- }
- }
- else
- {
- people.Add(tempName);
- }
- }
- }
- }
- if (people.Count > 0)
- {
- Console.WriteLine($"{String.Join(", ", people)} are going to the party!");
- }
- else
- {
- Console.WriteLine("Nobody is going to the party!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement