-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogs.cs
More file actions
89 lines (75 loc) · 2.65 KB
/
logs.cs
File metadata and controls
89 lines (75 loc) · 2.65 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
79
80
81
82
83
84
85
86
87
88
89
// в текстовом файле содержится лог ошибок от различных датчиков.
// записи сделаны в следующем формате
// функция получает имя файла и возвращает упорядоченный по алфавиту список датчиков, в которых были обнаружены ошибки
namespace logs
{
internal class Program
{
public struct sensor
{
public string name;
public List<int> errors;
}
static void Main(string[] args)
{
string nameOfFile = "log.txt";
List<sensor> ans = getListOfSensors(nameOfFile);
Console.WriteLine();
}
public static List<sensor> getListOfSensors(String nameOfFile)
{
List<sensor> ans = new List<sensor>();
StreamReader sr = new StreamReader(nameOfFile);
while (!sr.EndOfStream)
{
String line = sr.ReadLine();
String[] splitted = line.Split(":,".ToCharArray());
String name = splitted[0];
List<int> errors = new List<int>();
for (int i = 1; i < splitted.Length; i++)
{
errors.Add(int.Parse(splitted[i]));
}
if (errors.Count == 0) continue;
int position = binarySearch(ans, name);
if (ans.Count > position && ans[position].name == name)
{
ans[position].errors.AddRange(errors);
} else
{
ans.Insert(position,
new sensor()
{
name = name,
errors = errors
}
);
}
}
sr.Close();
return ans;
}
static int binarySearch(List<sensor> ans, String name)
{
int left = 0;
int right = ans.Count;
while (left < right - 1)
{
int mid = (left + right) / 2;
String current = ans[mid].name;
int compared = current.CompareTo(name);
if (compared == 0)
{
return mid;
} else if (compared < 0)
{
left = mid;
} else
{
right = mid;
}
}
return right;
}
}
}