-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmedian.php
More file actions
25 lines (23 loc) · 820 Bytes
/
median.php
File metadata and controls
25 lines (23 loc) · 820 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
<?php
// 7 kyu - Median fun fun
// Description:
//
// The mean (or average) is the most popular measure of central tendency; however it does not behave very well when the data is skewed (i.e. wages distribution). In such cases, it's better to use the median.
//
// Your task for this kata is to find the median of an array consisting of n elements.
//
// You can assume that all inputs are arrays of numbers in integer format. For the empty array your code should return NaN (false in JavaScript/NULL in PHP).
//
// Examples:
//
// Input [1, 2, 3, 4] --> Median 2.5
//
// Input [3, 4, 1, 2, 5] --> Median 3
function median($a) {
if (!count($a)) return NULL;
sort($a);
return count($a) % 2 ? $a[count($a)/2] : ($a[count($a)/2 - 1] + $a[count($a)/2])/2;
}
$answer = median([2, 1, 3, 4]);
print_r("$answer \n");
?>