Created
June 7, 2013 13:53
-
-
Save pepegar/5729411 to your computer and use it in GitHub Desktop.
Get Extremes of integer array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getExtremes($test) | |
{ | |
$sum = array_sum($test); | |
$average = $sum / count($test); | |
$deviations = array(); | |
$ret = array(); | |
for ($i = 0; $i < count($test); $i++) | |
{ | |
$deviation = $test[$i] - $average; | |
$deviation = abs($deviation); | |
$deviations[$i] = $deviation; | |
} | |
// Extreme value | |
$max = max($deviations); | |
foreach ($deviations as $deviation) | |
{ | |
if ($deviation == $max) { | |
$ret[] = $deviation; | |
} | |
} | |
if (count($ret) == 1) { | |
$ret = $ret[0]; | |
} | |
var_dump($ret); | |
} | |
// Set of arrays to test | |
$tests = array( | |
array( 1,2,3,4,5,6,7,8,89,9,0,45,3,-1,-1234,-1234,234,1234,1234, -234, 345, -345 ), | |
array( 1,2,3,4,5,6,7,8,89,9,0,45,3,-1,-1234), | |
); | |
// Loop the arrays to test each of them | |
foreach ($tests as $test) | |
{ | |
getExtremes($test); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment