-
Notifications
You must be signed in to change notification settings - Fork 17
/
basic.php
66 lines (55 loc) · 1.52 KB
/
basic.php
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
<?php
require_once __DIR__ . '/common.php';
function solution()
{
$file = fopen(MEASUREMENTS_PATH, 'r');
$arr = [];
while (($line = stream_get_line($file, 99, "\n")) !== false) {
$temp = explode(';', $line);
$key = $temp[0];
$value = (int)$temp[1];
$current_arr = &$arr[$key];
if ($current_arr !== null) {
if ($current_arr['max'] < $value) {
$current_arr['max'] = $value;
}
if ($current_arr['min'] > $value) {
$current_arr['min'] = $value;
}
++$current_arr['cnt'];
$current_arr['total'] += $value;
} else {
$arr[$key] = [
'max' => $value,
'min' => $value,
'cnt' => 1,
'total' => $value,
];
}
}
fclose($file);
ksort($arr);
$result = '';
foreach ($arr as $key => $value) {
$avg = (int)($value['total'] / $value['cnt']);
$result .= "$key={$value['min']};{$value['max']};$avg({$value['total']}/{$value['cnt']})\n";
}
return $result;
}
$start = microtime(true);
$got = solution();
$end = microtime(true);
$total_time = floor(($end - $start) * 1000);
echo "Elapsed {$total_time}ms\n";
// 검증
$expect_output = file_get_contents(OUTPUT_PATH);
if ($got === $expect_output) {
echo 'Test passed';
} else {
echo "Test failed\n";
echo 'Expected:';
echo $expect_output;
echo PHP_EOL;
echo 'Actual:';
echo $got;
}