Created
April 20, 2016 21:05
-
-
Save gpfiel/478ba0d72984ef3527d964b799bec539 to your computer and use it in GitHub Desktop.
Question3Toptal
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 solution($A) | |
{ | |
$sum = 0; | |
while (count($A)) { | |
// remove the last part | |
array_pop($A); | |
// remove and sum the first line | |
$sum += array_sum((array)array_shift($A)); | |
// remove and sum the last column | |
array_walk($A, function(&$line) use (&$sum) { | |
$sum += array_pop($line); | |
}); | |
// remove the first column | |
array_walk($A, function(&$line) use (&$sum) { | |
array_shift($line); | |
}); | |
$sum += array_sum((array)array_pop($A)); | |
// remove first line | |
array_shift($A); | |
// remove the last column | |
array_walk($A, function(&$line) use (&$sum) { | |
array_pop($line); | |
}); | |
// remove and sum the first column | |
array_walk($A, function(&$line) use (&$sum) { | |
$sum += array_shift($line); | |
}); | |
} | |
return $sum; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment