Created
June 18, 2015 12:07
-
-
Save yuklia/0c7ec0eb8ea484ee79bc to your computer and use it in GitHub Desktop.
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 | |
class Math | |
{ | |
public static function min($original) | |
{ | |
$i = 0; | |
$mi = $original[$i]; | |
for (; $i < count($original);) { | |
if ($original[$i] < $mi) { | |
$mi = $original[$i]; | |
} | |
$i++; | |
} | |
return $mi; | |
} | |
public static function max($original) | |
{ | |
$i = 0; | |
$ma = $original[$i]; | |
for (; $i < count($original);) { | |
if ($original[$i] > $ma) { | |
$ma = $original[$i]; | |
} | |
$i++; | |
} | |
return $ma; | |
} | |
public static function inArray($ar, $value) | |
{ | |
for ($i = 0; $i < count($ar); $i++) { | |
if ($ar[$i] == $value) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public static function fillSpaces($original, $min) | |
{ | |
for ($i = $min; $i < count($original); $i++) { //range | |
if (!Math::inArray($original, $i)) { | |
echo "$i"."</br>"; | |
} | |
} | |
} | |
public static function matches($original) | |
{ | |
$counter = 0; | |
foreach ($original as &$current) { | |
foreach ($original as $j => &$value) { | |
if ($value == $current) { | |
unset($original[$j]); | |
$counter++; | |
} | |
} | |
if ($counter >= 2) { | |
echo sprintf('%d appears %d times',$current,$counter) ."</br>"; | |
} | |
$counter = 0; | |
} | |
} | |
} //class | |
function main(){ | |
$test = [3, 1, -5, 3, 3, -5, 0, 10, 1, 1]; | |
echo sprintf("Range is %s to %s", Math::min($test), Math::max($test)) ."</br>"; | |
echo "Missing Numbers: </br>"; | |
Math::fillSpaces($test, Math::min($test)); | |
echo "Duplicate Numbers: </br>"; | |
Math::matches($test); | |
} | |
main(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment