Last active
November 4, 2018 20:34
-
-
Save iCaspar/a5da1c331fa28ff45609b3ccfe7f4185 to your computer and use it in GitHub Desktop.
Matrix Addition puzzle
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 | |
/** | |
* Unit tests for Matrix Addition puzzle. | |
* | |
* Requirements: @see https://www.codewars.com/kata/matrix-addition | |
* Write a function that accepts two square matrices (N x N two dimensional arrays), | |
* and return the sum of the two. | |
* Both matrices being passed into the function will be of size N x N (square), | |
* containing only integers. | |
* | |
* How to sum two matrices: | |
* Take each cell [n][m] from the first matrix, | |
* and add it with the same [n][m] cell from the second matrix. | |
* This will be cell [n][m] of the solution matrix. | |
* | |
* @package Cata\Tests\Unit\MatrixAddition | |
* @author Caspar Green <[email protected]> | |
* @since 1.0.0 | |
*/ | |
declare(strict_types=1); | |
namespace Cata\Tests\Unit\MatrixAddition; | |
use Cata\MatrixAddition\MatrixAdder; | |
use PHPUnit\Framework\TestCase; | |
/** | |
* Class MatrixAdderTest | |
* | |
* @package Cata\Tests\Unit\MatrixAddition | |
* @since 1.0.0 | |
*/ | |
class MatrixAdderTest extends TestCase | |
{ | |
/** | |
* A MatrixAdder instance. | |
* | |
* @var MatrixAdder | |
*/ | |
private $matrixAdder; | |
/** | |
* Set up before each test. | |
* | |
* @since 1.0.0 | |
* | |
* @return void | |
*/ | |
protected function setUp() | |
{ | |
$this->matrixAdder = new MatrixAdder(); | |
} | |
/** | |
* Test addMatrices() should return the matrix sum for (1x1) addends. | |
* | |
* @since 1.0.0 | |
* | |
* @return void | |
*/ | |
public function testAddMatricesShouldReturnMatrixSumFor1x1(): void | |
{ | |
$this->assertSame([[10]], $this->matrixAdder->addMatrices([[4]], [[6]])); | |
} | |
/** | |
* Test addMatrices() should return the matrix sum for (2x2) matrix. | |
* | |
* Once we have this, we have a working algorithm for (N x N). | |
* | |
* @since 1.0.0 | |
* | |
* @return void | |
*/ | |
public function testAddMatricesShouldReturnMatrixSumFor2x2(): void | |
{ | |
$addend1 = [ | |
[3, 5], | |
[4, 8], | |
]; | |
$addend2 = [ | |
[2, 7], | |
[6, 3], | |
]; | |
$this->assertSame( | |
[ | |
[5, 12], | |
[10, 11], | |
], | |
$this->matrixAdder->addMatrices($addend1, $addend2) | |
); | |
} | |
} | |
<?php | |
/** | |
* Matrix Adder puzzle class | |
* | |
* @package Cata\MatrixAddition | |
* @author Caspar Green <[email protected]> | |
* @since 1.0.0 | |
*/ | |
declare(strict_types=1); | |
namespace Cata\MatrixAddition; | |
/** | |
* Class MatrixAdder | |
* | |
* @package Cata\MatrixAddition | |
* @since 1.0.0 | |
*/ | |
class MatrixAdder | |
{ | |
/** | |
* Add two matrices of integers. | |
* | |
* @param array $matrix1 First addend matrix. | |
* @param array $matrix2 Second addend matrix. | |
* | |
* @since 1.0.0 | |
* | |
* @return array | |
*/ | |
public function addMatrices(array $matrix1, array $matrix2): array | |
{ | |
$sum = []; | |
foreach ($matrix1 as $row => $cols) { | |
foreach ($cols as $col => $value) { | |
$sum[$row][$col] = $matrix1[$row][$col] + $matrix2[$row][$col]; | |
} | |
} | |
return $sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment