Last active
August 14, 2018 12:54
-
-
Save jonathanlaf/e8781b74a9a5c33bf61cc1060ffac644 to your computer and use it in GitHub Desktop.
[Closest value operator] Get closest value (of an array) before a given one #php #operand
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 valueExpected ($analyse, $expect, $strict = false) | |
{ | |
if ($strict) | |
{ | |
if ($analyse === $expect) | |
{ | |
return '<span style="background-color:darkolivegreen; color:darkgrey;">Received : ' . | |
$analyse . | |
', Expected : ' . | |
$expect . | |
' > Test->Passed</span>'; | |
} | |
} | |
if (!$strict) | |
{ | |
if ($analyse == $expect) | |
{ | |
return '<span style="background-color:darkolivegreen; color:darkgrey;">Received : ' . | |
$analyse . | |
', Expected : ' . | |
$expect . | |
' > Test->Passed</span>'; | |
} | |
} | |
return '<span style="background-color:lightcoral; color:black;">Received : ' . | |
$analyse . | |
', Expected : ' . | |
$expect . | |
' > Test->Failed</span>'; | |
} | |
function getClosestLowerValue ($search, $haystack) | |
{ | |
$closest = null; | |
$testStack = $haystack; | |
$TotalLoop = count($testStack); | |
//Removed higher value again tested value in $testStack | |
for ($i = 0; $TotalLoop > $i; $i++) | |
{ | |
if ($testStack[ $i ] > $search) | |
{ | |
unset($testStack[ $i ]); | |
} | |
} | |
//If no value left in $testStack, rerun function with a search greater by twelve (searching for month) to move position on "infinite timeline" | |
if (empty($testStack)) | |
{ | |
$search = $search + 12; | |
return getClosestLowerValue($search, $haystack); | |
} | |
else | |
{ | |
//Since $testStack was not empty, we asume that maybe some key been removed, so we reassign position for a clean for loop. | |
$haystack = array_values($testStack); | |
$TotalLoop = count($haystack); | |
//Search for the nearest value | |
for ($i = 0; $TotalLoop > $i; $i++) | |
{ | |
if ($closest === null || abs($search - $closest) > abs($haystack[ $i ] - $search)) | |
{ | |
$closest = $haystack[ $i ]; | |
} | |
} | |
return $closest; | |
} | |
} | |
echo 'Testing 01 > ' . valueExpected(getClosestLowerValue(1, array (1, 5, 9, 12)), 1) . '<br>'; | |
echo 'Testing 02 > ' . valueExpected(getClosestLowerValue(2, array (1, 5, 9, 12)), 1) . '<br>'; | |
echo 'Testing 03 > ' . valueExpected(getClosestLowerValue(3, array (1, 5, 9, 12)), 1) . '<br>'; | |
echo 'Testing 04 > ' . valueExpected(getClosestLowerValue(4, array (1, 5, 9, 12)), 1) . '<br>'; | |
echo 'Testing 05 > ' . valueExpected(getClosestLowerValue(5, array (1, 5, 9, 12)), 5) . '<br>'; | |
echo 'Testing 06 > ' . valueExpected(getClosestLowerValue(6, array (1, 5, 9, 12)), 5) . '<br>'; | |
echo 'Testing 07 > ' . valueExpected(getClosestLowerValue(7, array (1, 5, 9, 12)), 5) . '<br>'; | |
echo 'Testing 08 > ' . valueExpected(getClosestLowerValue(8, array (1, 5, 9, 12)), 5) . '<br>'; | |
echo 'Testing 09 > ' . valueExpected(getClosestLowerValue(9, array (1, 5, 9, 12)), 9) . '<br>'; | |
echo 'Testing 10 > ' . valueExpected(getClosestLowerValue(10, array (1, 5, 9, 12)), 9) . '<br>'; | |
echo 'Testing 11 > ' . valueExpected(getClosestLowerValue(11, array (1, 5, 9, 12)), 9) . '<br>'; | |
echo 'Testing 12 > ' . valueExpected(getClosestLowerValue(12, array (1, 5, 9, 12)), 12) . '<br>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment