/
02.CommandInterpreter.cs
112 lines (93 loc) · 3.76 KB
/
02.CommandInterpreter.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
103
104
105
106
107
108
109
110
111
112
namespace _01.Some_Exam
{
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
string inputLine = Console.ReadLine();
List<string> input = inputLine.Split().ToList();
string commands = Console.ReadLine();
while (commands != "end")
{
var tokens = commands.Split().ToArray();
switch (tokens[0])
{
case "reverse":
int startReverse = int.Parse(tokens[2]);
int countReverse = int.Parse(tokens[4]);
if (startReverse >= 0 &&
startReverse < input.Count &&
countReverse >= 0 &&
startReverse + countReverse <= input.Count)
{
input.Reverse(startReverse, countReverse);
}
else
{
Console.WriteLine("Invalid input parameters.");
}
break;
case "sort":
int startSort = int.Parse(tokens[2]);
int countSort = int.Parse(tokens[4]);
if (startSort >= 0 &&
startSort < input.Count &&
countSort >= 0 &&
startSort + countSort <= input.Count)
{
input.Sort(startSort, countSort, StringComparer.InvariantCulture);
}
else
{
Console.WriteLine("Invalid input parameters.");
}
break;
case "rollLeft":
int countRollLeft = int.Parse(tokens[1]);
if (countRollLeft >= 0)
{
for (int i = 0; i < countRollLeft % input.Count; i++)
{
string firstElement = input[0];
for (int j = 0; j < input.Count - 1; j++)
{
input[j] = input[j + 1];
}
input[input.Count - 1] = firstElement;
}
}
else
{
Console.WriteLine("Invalid input parameters.");
}
break;
case "rollRight":
int countRollRight = int.Parse(tokens[1]);
if (countRollRight >= 0)
{
for (int i = 0; i < countRollRight % input.Count; i++)
{
string lastElement = input[input.Count - 1];
for (int j = input.Count - 1; j > 0; j--)
{
input[j] = input[j - 1];
}
input[0] = lastElement;
}
}
else
{
Console.WriteLine("Invalid input parameters.");
}
break;
}
commands = Console.ReadLine();
}
Console.WriteLine("[" + (string.Join(", ", input)) + "]");
}
}
}