Last active
October 5, 2018 14:46
-
-
Save zgoniaiko/017f18cf85d6011c0c762da8a889244b to your computer and use it in GitHub Desktop.
Function to find the maximum value in a nested set of arrays
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 | |
namespace App\Entity; | |
class MyMax | |
{ | |
public function find(array $input) { | |
$max = null; | |
foreach ($input as $value) { | |
$result = null; | |
if (is_array($value)) { | |
$result = $this->find($value); | |
} | |
if (is_numeric($value)) { | |
$result = $value; | |
} | |
$max = ($result > $max) ? $result : $max; | |
} | |
return $max; | |
} | |
} |
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 | |
namespace App\Tests; | |
use App\Entity\MyMax; | |
use PHPUnit\Framework\TestCase; | |
class MyMaxTest extends TestCase | |
{ | |
public function testPlainArray() | |
{ | |
$finder = new MyMax(); | |
$this->assertNull($finder->find([])); | |
$this->assertEquals(1, $finder->find([1])); | |
$this->assertEquals(2, $finder->find([1, 2])); | |
$this->assertEquals(3, $finder->find([1, 1 + 2])); | |
} | |
public function testNestedArray() | |
{ | |
$finder = new MyMax(); | |
$this->assertNull($finder->find([[]])); | |
$this->assertEquals(2, $finder->find([1, [1, 2]])); | |
$this->assertEquals(3, $finder->find([3, [1, 2]])); | |
$this->assertEquals(3, $finder->find([3, [1, 2], [2, 3]])); | |
$this->assertEquals(4, $finder->find([3, [1, 2], [4, 3]])); | |
$this->assertEquals(5, $finder->find([3, [1, 2, [4, 5]], [4, 3]])); | |
$this->assertEquals(1000, $finder->find([1, 2, [3, [1000, 4, 5], 6, 7], 8, 9])); | |
$this->assertEquals(1000, $finder->find([1, 2, [3, [1000, 4, 5, '999'], 6, 7], 8, 9])); | |
$this->assertEquals(1000, $finder->find([1, 2, [3, ['1000', 4, 5], 6, 7], 8, 9])); | |
$this->assertEquals(1000, $finder->find([1, 2, [3, ['key' => 1000, 4, 5], 6, 7], 8, 9])); | |
$this->assertEquals(1000, $finder->find([1, 2, [3, ['key' => '1000', 4, 5], 6, 7], 8, 9])); | |
$this->assertEquals(1000, $finder->find([1, 2, [3, [1000, 4, 5, new \stdClass()], 6, 7], 8, 9])); | |
} | |
public function testFloat() | |
{ | |
$finder = new MyMax(); | |
$this->assertEquals(1, $finder->find([1, 0.5])); | |
$this->assertEquals(1, $finder->find([1, 0.5])); | |
} | |
public function testInfinity() | |
{ | |
$finder = new MyMax(); | |
$this->assertEquals(INF, $finder->find([1, INF])); | |
$this->assertEquals(1, $finder->find([1, -INF])); | |
$this->assertEquals(1, $finder->find([1, log(0)])); | |
} | |
public function testNan() | |
{ | |
$finder = new MyMax(); | |
$this->assertEquals(1, $finder->find([1, NAN])); | |
$this->assertEquals(-1, $finder->find([-1, NAN])); | |
} | |
public function testNull() | |
{ | |
$finder = new MyMax(); | |
$this->assertNull($finder->find([null])); | |
$this->assertEquals(1, $finder->find([1, null])); | |
$this->assertEquals(-1, $finder->find([-1, null])); | |
} | |
public function testNegative() | |
{ | |
$finder = new MyMax(); | |
$this->assertEquals(-1, $finder->find([-1, -2])); | |
$this->assertEquals(-1, $finder->find(['-1', -2])); | |
} | |
public function testString() | |
{ | |
$finder = new MyMax(); | |
$this->assertEquals(1, $finder->find([1, 'a'])); | |
$this->assertEquals(1, $finder->find([1, '22a'])); | |
$this->assertEquals(1, $finder->find([1, 'a22'])); | |
$this->assertEquals(1000, $finder->find([1, 2, [3, [1000, 4, 5, 'test'], 6, 7], 8, 9])); | |
} | |
} |
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 my_max(array $xs) { | |
$max = null; | |
foreach ($xs as $value) { | |
$result = null; | |
if (is_array($value)) { | |
$result = my_max($value); | |
} | |
if (is_numeric($value)) { | |
$result = $value; | |
} | |
$max = ($result > $max) ? $result : $max; | |
} | |
return $max; | |
} | |
echo my_max([1, 2, [3, [1000, 4, 5], 6, 7], 8, 9]) . "\n"; // 1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment