-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.php
More file actions
43 lines (33 loc) · 890 Bytes
/
4.php
File metadata and controls
43 lines (33 loc) · 890 Bytes
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
<?php
/*
* Complete the 'plusMinus' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/
function getStat($number, $total) {
$result = $number / $total;
return number_format($result, 6, '.', '');
}
function plusMinus($arr) {
// Write your code here
$stats['positives'] = 0;
$stats['negatives'] = 0;
$stats['zero']= 0;
$length = count($arr);
foreach($arr as $item) {
if ($item > 0) {
++$stats['positives'];
} elseif($item == 0) {
++$stats['zero'];
} else {
++$stats['negatives'];
}
}
foreach($stats as $k => $v) {
echo getStat($v, $length) . PHP_EOL;
}
}
$n = intval(trim(fgets(STDIN)));
$arr_temp = rtrim(fgets(STDIN));
$arr = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
plusMinus($arr);