mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
BUGFIX: median function and median tests, fix Issue #102
This commit is contained in:
parent
9b9963ebf6
commit
da64ca9207
@ -16,7 +16,10 @@ function median(...$numbers)
|
||||
sort($numbers);
|
||||
$length = count($numbers);
|
||||
|
||||
$middle = $length >> 1;
|
||||
$median = ($numbers[$middle] + $numbers[$middle - 1]) / 2;
|
||||
return $median;
|
||||
$middle = ceil($length / 2);
|
||||
if($length % 2 == 0) {
|
||||
return ($numbers[$middle] + $numbers[$middle - 1]) / 2;
|
||||
}
|
||||
|
||||
return $numbers[$middle - 1];
|
||||
}
|
@ -134,9 +134,9 @@ class MathsTest extends TestCase
|
||||
|
||||
public function testMedian()
|
||||
{
|
||||
median(1,2,8,11);
|
||||
median(40,90);
|
||||
median(35,66,91,27);
|
||||
assertEquals(3, median(1, 2, 3, 4, 5));
|
||||
assertEquals(4.5, median(1, 2, 3, 4, 5, 6, 7, 8));
|
||||
assertEquals(3, median(5, 3, 1, 2, 4));
|
||||
}
|
||||
|
||||
public function testMode()
|
||||
|
Loading…
x
Reference in New Issue
Block a user