-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseOperations.cs
More file actions
78 lines (71 loc) · 2.92 KB
/
reverseOperations.cs
File metadata and controls
78 lines (71 loc) · 2.92 KB
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
// Функция получает на вход два списка (тип List). Один список содержит информационные элементы - это числа типа Integer.
// Второй список содержит информацию об операциях, которые были выполнены с элементами первого списка.
// Возможные операции: удаление элемента, добавление элемента в определенную позицию, изменение значения элемента.
// необходимо, чтобы ваша функция вернула список в исходное состояние, отменив выполненные с ним действия
namespace reverseGroundMeat
{
internal class Program
{
public enum OperationsName {
Delete,
Add,
Replace
}
public struct Operation
{
public OperationsName name;
public int whichElement;
public int placeOfElement;
public int toWhichElement;
}
static void Main(string[] args)
{
List<int> toReverse = new List<int>() { 1, 3, 2, 6, 5 };
List<Operation> operations = new List<Operation>() {
new Operation {
name=OperationsName.Delete,
placeOfElement=2,
whichElement=3
},
new Operation
{
name=OperationsName.Replace,
placeOfElement=2,
whichElement=4,
toWhichElement=6
},
new Operation {
name =OperationsName.Add,
whichElement=3,
placeOfElement=1
}
};
List<int> reversed = reverse(toReverse, operations);
foreach (int elt in reversed)
{
Console.Write($"{elt} ");
}
Console.WriteLine();
}
static public List<int> reverse(List<int> toReverse, List<Operation> operations)
{
List<int> ans = new List<int>(toReverse);
for (int i = operations.Count-1; i >= 0; i--)
{
if (operations[i].name == OperationsName.Delete)
{
ans.Insert(operations[i].placeOfElement, operations[i].whichElement);
}
if (operations[i].name == OperationsName.Replace)
{
ans[operations[i].placeOfElement] = operations[i].whichElement;
}
if (operations[i].name == OperationsName.Add)
{
ans.RemoveAt(operations[i].placeOfElement);
}
}
return ans;
}
}
}