Created
February 1, 2016 04:49
-
-
Save ReeSilva/052627fa5ddd46928f99 to your computer and use it in GitHub Desktop.
Spiral
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 lisbonInterviewEx1 | |
{ | |
public $finalArray = array(); | |
public function callSpiral($matrix) { | |
$arithmeticalNum = 1; | |
$numRows = count($matrix); | |
$numCols = count($matrix[0]); | |
$centerCol = $this->calculateCenter($numCols); | |
$centerRow = $this->calculateCenter($numRows); | |
array_push($this->finalArray, $matrix[$centerRow][$centerCol]); | |
$this->makeSpiral($arithmeticalNum, $matrix, $numCols, $centerRow, $centerCol); | |
print_r($this->finalArray); | |
} | |
private function makeSpiral($arithmeticalNum, $matrix, $numRows, $centerRow, $centerCol) { | |
if ($arithmeticalNum %2 == 0) { | |
for ($i = 1; $i <= $arithmeticalNum; $i++) { | |
$centerCol++; | |
array_push($this->finalArray, $matrix[$centerRow][$centerCol]); | |
} | |
for ($i = 1; $i <= $arithmeticalNum; $i++) { | |
$centerRow++; | |
array_push($this->finalArray, $matrix[$centerRow][$centerCol]); | |
} | |
} else { | |
for ($i = 1; $i <= $arithmeticalNum; $i++) { | |
$centerCol--; | |
array_push($this->finalArray, $matrix[$centerRow][$centerCol]); | |
} | |
for ($i = 1; $i <= $arithmeticalNum; $i++) { | |
$centerRow--; | |
array_push($this->finalArray, $matrix[$centerRow][$centerCol]); | |
} | |
} | |
if ($arithmeticalNum == $numRows - 1) { | |
for ($i = 1; $i <= $arithmeticalNum; $i++) { | |
$centerCol--; | |
array_push($this->finalArray, $matrix[$centerRow][$centerCol]); | |
} | |
} | |
$arithmeticalNum++; | |
if ($arithmeticalNum < $numRows) { | |
return $this->makeSpiral($arithmeticalNum, $matrix, $numRows, $centerRow, $centerCol); | |
} | |
} | |
private function calculateCenter($total) { | |
$center = $total/2; | |
return $center; | |
} | |
} | |
$array = array( | |
array( 1,34, 32, 2, 4), | |
array( 3,32, 13,19,89), | |
array(56,11, 7, 9,95), | |
array(14,90,333,23, 1), | |
array( 5,33, 54,23,41) | |
); | |
$lisbonInterview = new lisbonInterviewEx1; | |
$lisbonInterview->callSpiral($array); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment