Skip to content

Instantly share code, notes, and snippets.

@pashamray
Created June 14, 2018 11:14
Show Gist options
  • Save pashamray/f75d8f8d67c5b58a4f5ae0ddc744bb0a to your computer and use it in GitHub Desktop.
Save pashamray/f75d8f8d67c5b58a4f5ae0ddc744bb0a to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: ps
* Date: 14.06.18
* Time: 9:23
*/
$ranges = [
6000 => 1986,
5500 => 1498,
5400 => 1286,
5300 => 1138,
5200 => 926,
5100 => 830,
5000 => 679,
4900 => 614,
4800 => 522,
4500 => 422,
4000 => 346,
3500 => 254,
2500 => 218,
1500 => 178
];
$min = 1;
$max = 6000;
function calc_range($min, $max, $ranges)
{
ksort($ranges);
$lesses = array_filter($ranges, function ($value, $key) use ($min) { return $key < $min; }, ARRAY_FILTER_USE_BOTH);
$greater = array_filter($ranges, function ($value, $key) use ($max) { return $key > $max; }, ARRAY_FILTER_USE_BOTH);
$ranges = array_diff_key($ranges, $lesses);
$ranges = array_diff_key($ranges, $greater);
$ret = 0;
$tmp = $min;
foreach ($ranges as $range => $coefficient) {
$ret += ($range - $tmp) * $coefficient;
$tmp = $range;
if($range >= $max) {
$ret += $range - $max;
}
}
return $ret / 100;
}
echo calc_range($min, $max, $ranges);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment