This repository was archived by the owner on Mar 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
102 lines (90 loc) · 3.53 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Linq;
namespace _9._Predicate_Party
{
internal class Program
{
static void Main(string[] args)
{
List<string> names = new List<string>(Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries));
Func<List<string>, string, List<string>> startsWith = (list, command) => list.FindAll(x => x.StartsWith(command));
Func<List<string>, string, List<string>> endsWith = (list, command) => list.FindAll(x => x.EndsWith(command));
Func<List<string>, string, List<string>> changeLength = (list, command) => list.FindAll(x => x.Length == int.Parse(command));
string command = Console.ReadLine();
while (command != "Party!")
{
string[] tokens = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
FilterNames(names, startsWith, endsWith, changeLength, tokens);
command = Console.ReadLine();
}
PrintNames(names);
}
static void FilterNames(List<string> names, Func<List<string>, string, List<string>> startsWith, Func<List<string>, string, List<string>> endsWith, Func<List<string>, string, List<string>> changeLength, string[] tokens)
{
switch (tokens[1])
{
case "StartsWith":
{
FilterNameStartsWith(names, startsWith, tokens);
break;
}
case "EndsWith":
{
FilterNameEndsWith(names, endsWith, tokens);
break;
}
case "Length":
{
FilterLengthOfName(names, changeLength, tokens);
break;
}
}
}
static void FilterNameStartsWith(List<string> names, Func<List<string>, string, List<string>> startsWith, string[] tokens)
{
if (tokens[0] == "Double")
{
names.AddRange(startsWith(names, tokens[2]));
}
else if (tokens[0] == "Remove")
{
startsWith(names, tokens[2]).ForEach(x => names.Remove(x));
}
}
static void FilterNameEndsWith(List<string> names, Func<List<string>, string, List<string>> endsWith, string[] tokens)
{
if (tokens[0] == "Double")
{
names.AddRange(endsWith(names, tokens[2]));
}
else if (tokens[0] == "Remove")
{
endsWith(names, tokens[2]).ForEach(x => names.Remove(x));
}
}
static void FilterLengthOfName(List<string> names, Func<List<string>, string, List<string>> changeLength, string[] tokens)
{
if (tokens[0] == "Double")
{
names.AddRange(changeLength(names, tokens[2]));
}
else if (tokens[0] == "Remove")
{
changeLength(names, tokens[2]).ForEach(x => names.Remove(x));
}
}
static void PrintNames(List<string> names)
{
Action<List<string>> print = list => Console.WriteLine($"{string.Join(", ", list)} are going to the party!");
if (names.Count != 0)
{
print(names);
}
else
{
Console.WriteLine("Nobody is going to the party!");
}
}
}
}