BUGFIX: median function and median tests, fix Issue #102

This commit is contained in:
80Quattro 2023-02-02 17:21:33 +01:00
parent 9b9963ebf6
commit da64ca9207
2 changed files with 9 additions and 6 deletions

View File

@ -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];
}

View File

@ -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()