Last active
August 26, 2019 16:53
-
-
Save fokosun/fc9ff416cbd2c49c0bffd228e0821630 to your computer and use it in GitHub Desktop.
watermelon challenge
This file contains hidden or 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 | |
/* | |
* ROOM FOR IMPROVEMENT | |
* both halves have to be divisible by 2 | |
*/ | |
function watermelon($n) { | |
$ans = array(); | |
if(is_integer($n)) { | |
$parts = range(1, ($n - 1)); | |
foreach($parts as $part) { | |
if ($part%2 === 0) { | |
$remainder = $n - $part; | |
if ($remainder%2 === 0 | |
&& ($remainder != $part) | |
&& $remainder > $part | |
) { | |
$ans[] = array($part, $remainder); | |
} | |
if($remainder == $part) { | |
$ans[] = array($part, $remainder); | |
} | |
} | |
} | |
} | |
return array_map(function($n) { | |
return '(' .$n[0] . ',' . $n[1] . ')'; | |
}, $ans); | |
} | |
var_dump(watermelon(8)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment