mean, median, mode (#83)

* mean median mode

* update DIRECTORY

* just renaming

* adding unit tests

* organizing the file

* ops missed this one
This commit is contained in:
tales 2022-10-16 01:41:56 -03:00 committed by GitHub
parent 89b2c36963
commit 6a67bf2606
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 4 deletions

View File

@ -24,6 +24,9 @@
* [Fibonacci2](./Maths/Fibonacci2.php)
* [Neonnumber](./Maths/NeonNumber.php)
* [Perfectsquare](./Maths/PerfectSquare.php)
* [Mean](./Maths/Mean.php)
* [Median](./Maths/Median.php)
* [Mode](./Maths/Mode.php)
* Projecteuler
* [Problem1](./Maths/ProjectEuler/Problem1.php)
* [Problem2](./Maths/ProjectEuler/Problem2.php)

20
Maths/Mean.php Normal file
View File

@ -0,0 +1,20 @@
<?php
/**
* This function calculates
* The mean value of
* numbers provided
*
* @param decimal $numbers A variable sized number input
* @return decimal $mean Mean of provided numbers
*/
function mean(...$numbers)
{
if (empty($numbers)) {
throw new \Exception('Please pass values to find mean value');
}
$total = array_sum($numbers);
$mean = $total / count($numbers);
return $mean;
}

22
Maths/Median.php Normal file
View File

@ -0,0 +1,22 @@
<?php
/**
* This function calculates
* The median value of
* numbers provided
*
* @param decimal $numbers A variable sized number input
* @return decimal $median Median of provided numbers
*/
function median(...$numbers)
{
if (empty($numbers)) {
throw new \Exception('Please pass values to find mean value');
}
sort($numbers);
$length = count($numbers);
$middle = $length >> 1;
$median = ($numbers[$middle] + $numbers[$middle - 1]) / 2;
return $median;
}

20
Maths/Mode.php Normal file
View File

@ -0,0 +1,20 @@
<?php
/**
* This function calculates
* The mode value of
* numbers provided
*
* @param decimal $numbers A variable sized number input
* @return decimal $mode Mode of provided numbers
*/
function mode(...$numbers)
{
if (empty($numbers)) {
throw new \Exception('Please pass values to find mean value');
}
$values = array_count_values($numbers);
$mode = array_search(max($values), $values);
return $mode;
}

View File

@ -17,6 +17,9 @@ require_once __DIR__ . '/../../Maths/Fibonacci.php';
require_once __DIR__ . '/../../Maths/Fibonacci2.php';
require_once __DIR__ . '/../../Maths/NeonNumber.php';
require_once __DIR__ . '/../../Maths/PerfectSquare.php';
require_once __DIR__ . '/../../Maths/Mean.php';
require_once __DIR__ . '/../../Maths/Median.php';
require_once __DIR__ . '/../../Maths/Mode.php';
class MathsTest extends TestCase
{
@ -29,8 +32,7 @@ class MathsTest extends TestCase
factorial(-25);
}
public function testIsNumberArmstrong()
public function testIsNumberArmstrong()
{
assertTrue(isNumberArmstrong(153));
assertFalse(isNumberArmstrong(123));
@ -38,8 +40,7 @@ public function testIsNumberArmstrong()
assertFalse(isNumberArmstrong(2468));
}
public function testIsNumberPalindromic()
public function testIsNumberPalindromic()
{
assertTrue(isNumberPalindromic(121));
assertFalse(isNumberPalindromic(123));
@ -106,6 +107,7 @@ public function testIsNumberPalindromic()
assertFalse(isNumberNeon(123));
assertTrue(isNumberNeon(9));
}
public function testFibonacciGenerator()
{
assertEquals([0, 1, 1, 2, 3], iterator_to_array(loop(5, fib())));
@ -114,4 +116,25 @@ public function testIsNumberPalindromic()
assertEquals([0, 1, 1, 2, 3], iterator_to_array(loop(5, fib())));
assertEquals([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], iterator_to_array(loop(10, fib())));
}
public function testMean()
{
mean(2,3,9,6);
mean(7);
mean(42,77,89,35);
}
public function testMedian()
{
median(1,2,8,11);
median(40,90);
median(35,66,91,27);
}
public function testMode()
{
mode(4,2,7,9,2,9,0,4,2);
mode(21,72,40,21,0,99,21,99,0,40);
mode(3,3,3,3);
}
}