Skip to content

Instantly share code, notes, and snippets.

@fokosun
Last active August 26, 2019 16:53
Show Gist options
  • Save fokosun/fc9ff416cbd2c49c0bffd228e0821630 to your computer and use it in GitHub Desktop.
Save fokosun/fc9ff416cbd2c49c0bffd228e0821630 to your computer and use it in GitHub Desktop.
watermelon challenge
<?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