[ create a new paste ] login | about

Link: http://codepad.org/PZVNE9Td    [ raw code | output | fork ]

PHP, pasted on Apr 30:
<?php

$prices = array
(
	'bar' => array(12.34, 102.55),
	'foo' => array(12.34, 15.66, 102.55, 134.66),
	'foobar' => array(12.34, 15.22, 14.18, 20.55, 99.50, 15.88, 16.99, 102.55),
);

foreach ($prices as $item => $bids)
{
	$average = call_user_func_array('Average', $bids);
	$standardDeviation = call_user_func_array('standardDeviation', $bids);

	foreach ($bids as $key => $bid)
	{
		if (($bid < ($average - $standardDeviation)) || ($bid > ($average + $standardDeviation)))
		{
			unset($bids[$key]);
		}
	}

	$prices[$item] = $bids;
}

print_r($prices);

function Average()
{
	if (count($arguments = func_get_args()) > 0)
	{
		return array_sum($arguments) / count($arguments);
	}

	return 0;
}

function standardDeviation()
{
	if (count($arguments = func_get_args()) > 0)
	{
		$result = call_user_func_array('Average', $arguments);

		foreach ($arguments as $key => $value)
		{
			$arguments[$key] = pow($value - $result, 2);
		}

		return sqrt(call_user_func_array('Average', $arguments));
	}

	return 0;
}

?>


Output:
Array
(
    [bar] => Array
        (
            [0] => 12.34
            [1] => 102.55
        )

    [foo] => Array
        (
            [1] => 15.66
            [2] => 102.55
        )

    [foobar] => Array
        (
            [0] => 12.34
            [1] => 15.22
            [2] => 14.18
            [3] => 20.55
            [5] => 15.88
            [6] => 16.99
        )

)


Create a new paste based on this one


Comments: