Created
June 23, 2024 03:44
-
-
Save defrindr/2159650e94ae915d41addd515ab1784d to your computer and use it in GitHub Desktop.
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 ChainableArray | |
{ | |
// Provided default array | |
protected $defaultArray = [ | |
['1', '2', '3', '4', '5', '6', '7', '8', '9', '0',], | |
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',], | |
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',], | |
['z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',], | |
]; | |
protected $resultArray = []; | |
public function __construct() | |
{ | |
// Given default value to result array | |
$this->resultArray = $this->defaultArray; | |
} | |
/** | |
* This transformation will flip all columns of the keyboard vertically (e.g., the 1 will swap with the z, the q with the a, the 2 with the x, etc.) | |
* @return ChainableArray | |
*/ | |
public function moveVertical(): ChainableArray | |
{ | |
$this->resultArray = array_reverse($this->resultArray); | |
return $this; | |
} | |
/** | |
* This transformation will flip all rows of the keyboard horizontally (e.g., the 1 will swap with the 0, the 2 with the 9, etc.) | |
* @return ChainableArray | |
*/ | |
public function moveHorizontal(): ChainableArray | |
{ | |
foreach ($this->resultArray as $parentKey => $childrenArray) { | |
$this->resultArray[$parentKey] = array_reverse($childrenArray); | |
} | |
return $this; | |
} | |
/** | |
* This transformation should take in an integer N and perform a linear shift of the Ke boa key ache ve past his platest it nits hold shit in i eise the and o | |
* on. So for example, for N = 5, the last five keys (nm,./ would move into the first 5 places of the | |
* top row, the 12345 would move 5 places to the right, 67890 would move to the start of the 2nd row, and so on). Likewise, left-shifting keys past their current rows would shift them back into the row above. Therefore, a single right and a left shift would produce the same keyboard. | |
* @return ChainableArray | |
*/ | |
public function shift(int $numberOfFlip): ChainableArray | |
{ | |
$newResult = []; | |
$lengthOfArray = count($this->resultArray); | |
$lengthOfChildren = count($this->resultArray[0]); | |
$lengthOfItem = $lengthOfChildren * $lengthOfArray; | |
// reverse number | |
$numberOfFlip *= -1; | |
for ($index = 0; $index < $lengthOfItem; $index++) { | |
$parent = $this->_indexOfMultiDimensionalArray($index, $lengthOfChildren, $lengthOfArray); | |
$numberTransform = $this->_transformIndex($index, $numberOfFlip, $lengthOfItem); | |
$children = $this->_indexOfMultiDimensionalArray($numberTransform, $lengthOfChildren, $lengthOfArray); | |
$newResult[$parent['parent']][$parent['children']] = $this->resultArray[$children['parent']][$children['children']]; | |
} | |
$this->resultArray = $newResult; | |
return $this; | |
} | |
/** | |
* Display the array | |
* @return void | |
*/ | |
public function display() | |
{ | |
foreach ($this->resultArray as $child) { | |
foreach ($child as $item) { | |
echo $item . " "; | |
} | |
echo "\n"; | |
}; | |
} | |
/** | |
* Function handle shifting index | |
* @return int | |
*/ | |
private function _transformIndex(int $index, int $numberOfFlip, int $lengthOfItem): int | |
{ | |
$numberTransform = $index + $numberOfFlip; | |
if ($numberTransform < 0) { | |
while ($numberTransform < 0) { | |
$numberTransform += $lengthOfItem; | |
} | |
} else if ($numberTransform > $lengthOfItem) { | |
$numberOfFlip %= $lengthOfItem; | |
} | |
return $numberTransform; | |
} | |
/** | |
* Convert index to multidimentional key | |
* @return int | |
*/ | |
private function _indexOfMultiDimensionalArray(int $index, int $childrenLength, int $arrayLength) | |
{ | |
$children = $index % $childrenLength; | |
$parent = floor($index / $childrenLength) % $arrayLength; | |
return compact('parent', 'children'); | |
} | |
} | |
$instance = new ChainableArray; | |
$instance->moveHorizontal()->moveVertical()->display(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment