forked from myyrakle/billion_row_challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
75 lines (61 loc) · 1.95 KB
/
Program.cs
File metadata and controls
75 lines (61 loc) · 1.95 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
using System.Diagnostics;
using System.Text;
const string OUTPUT_PATH = "outputs.txt";
const string MEASUREMENTS_PATH = "measurements.txt";
var expectOutput = File.ReadAllText(OUTPUT_PATH);
var stopwatch = Stopwatch.StartNew();
var got = Solution();
stopwatch.Stop();
Console.WriteLine($"Elapsed time: {stopwatch.ElapsedMilliseconds} ms");
if (got == expectOutput)
{
Console.WriteLine("Passed");
}
else
{
Console.WriteLine("Failed");
Console.WriteLine($"Expected:\n{expectOutput}");
Console.WriteLine($"Got:\n{got}");
}
string Solution()
{
var hashmap = new Dictionary<string, Status>();
using (var reader = new StreamReader(MEASUREMENTS_PATH))
{
string? line;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(';');
string cityName = parts[0];
int measurement = int.Parse(parts[1]);
if (!hashmap.ContainsKey(cityName))
{
hashmap[cityName] = new Status
{
min = measurement,
max = measurement,
total = measurement,
count = 1
};
}
else
{
hashmap[cityName].min = Math.Min(hashmap[cityName].min, measurement);
hashmap[cityName].max = Math.Max(hashmap[cityName].max, measurement);
hashmap[cityName].total += measurement;
hashmap[cityName].count++;
}
}
}
// Get Key List, then sort it
var keyList = hashmap.Keys.ToList();
keyList.Sort((a, b) => string.Compare(a, b, StringComparison.Ordinal));
var result = new StringBuilder();
foreach (var key in keyList)
{
var status = hashmap[key];
var avg = status.total / status.count;
result.Append($"{key}={status.min};{status.max};{avg}({status.total}/{status.count})\n");
}
return result.ToString();
}