From c5493e544af354e69d3d66ba563bd14ef7c8f9be Mon Sep 17 00:00:00 2001 From: Jeff Sheffel Date: Thu, 1 Jun 2017 21:25:42 -0600 Subject: [PATCH 1/7] add composer and gitignore --- .gitignore | 2 ++ composer.json | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a9875b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +composer.lock diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..092c746 --- /dev/null +++ b/composer.json @@ -0,0 +1,16 @@ +{ + "name": "jscodefix/codetest", + "description": "Fork of Booj PHP code test.", + "require": { + }, + "require-dev": { + "phpunit/phpunit": "^6.1" + }, + "authors": [ + { + "name": "Jeff Sheffel", + "email": "jeff@sheffel.org" + } + ], + "minimum-stability": "dev" +} From 3d387c66ccefd987d812f6fefc1968e18f85d516 Mon Sep 17 00:00:00 2001 From: Jeff Sheffel Date: Fri, 2 Jun 2017 09:10:13 -0600 Subject: [PATCH 2/7] restructure and fix test code for phpunit 6.1 compliance --- EquilibriumIndex.php | 8 +++++--- EquilibriumIndexTest.php | 12 ------------ Test/EquilibriumIndexTest.php | 17 +++++++++++++++++ phpunit.xml.dist | 13 +++++++++++++ 4 files changed, 35 insertions(+), 15 deletions(-) delete mode 100644 EquilibriumIndexTest.php create mode 100644 Test/EquilibriumIndexTest.php create mode 100644 phpunit.xml.dist diff --git a/EquilibriumIndex.php b/EquilibriumIndex.php index 020dd2b..6077387 100644 --- a/EquilibriumIndex.php +++ b/EquilibriumIndex.php @@ -1,9 +1,11 @@ assertEquals(array(3,6), getEquilibriums($arr)); - } -} diff --git a/Test/EquilibriumIndexTest.php b/Test/EquilibriumIndexTest.php new file mode 100644 index 0000000..f46408e --- /dev/null +++ b/Test/EquilibriumIndexTest.php @@ -0,0 +1,17 @@ +assertEquals(array(3,6), Booj\CodeTest\getEquilibriums($arr)); + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..48663df --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,13 @@ + + + + + ./Test + + + + + ./vendor + + + From 1e75ed89060ef6370489decd0b674acd0c183f89 Mon Sep 17 00:00:00 2001 From: Jeff Sheffel Date: Fri, 2 Jun 2017 09:11:07 -0600 Subject: [PATCH 3/7] add .idea/ to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3a9875b..2234772 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ composer.lock +.idea/ From 2727922e55373ec0c5736f111a32783166d98be6 Mon Sep 17 00:00:00 2001 From: Jeff Sheffel Date: Fri, 2 Jun 2017 16:46:04 -0600 Subject: [PATCH 4/7] minor updates to unit test and readme --- README.md | 3 ++- Test/EquilibriumIndexTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c88ac00..96908d6 100644 --- a/README.md +++ b/README.md @@ -10,5 +10,6 @@ Write a function that, given a sequence, returns its equilibrium indices (if any ## Running the Test ``` -phpunit EquilibriumIndexTest.php +composer install +vendor/phpunit/phpunit/phpunit ``` diff --git a/Test/EquilibriumIndexTest.php b/Test/EquilibriumIndexTest.php index f46408e..0147671 100644 --- a/Test/EquilibriumIndexTest.php +++ b/Test/EquilibriumIndexTest.php @@ -2,16 +2,16 @@ //namespace Booj\CodeTest\Tests; +require_once("EquilibriumIndex.php"); + use Booj\CodeTest; use PHPUnit\Framework\TestCase; -require_once("./EquilibriumIndex.php"); - class EquilibriumIndexTest extends TestCase { public function testComputeEquilibrium() { - $arr = array(-7, 1, 5, 2, -4, 3, 0); - $this->assertEquals(array(3,6), Booj\CodeTest\getEquilibriums($arr)); + $input = [-7, 1, 5, 2, -4, 3, 0]; + $this->assertEquals(array(3,6), Booj\CodeTest\getEquilibriums($input)); } } From 94a39701cb8973f0be44041612d7252879d40369 Mon Sep 17 00:00:00 2001 From: Jeff Sheffel Date: Fri, 2 Jun 2017 16:47:36 -0600 Subject: [PATCH 5/7] initial solution to equilibrium index problem --- EquilibriumIndex.php | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/EquilibriumIndex.php b/EquilibriumIndex.php index 6077387..6c1ef51 100644 --- a/EquilibriumIndex.php +++ b/EquilibriumIndex.php @@ -2,10 +2,44 @@ namespace Booj\CodeTest; -# Put a valid docblock here! -function getEquilibriums($arr) { - $output = array(); - # Logic goes here! - return $output; +/** + * This function ... + * + * @param $input array to + * @return array + */ +function getEquilibriums(array $input) { + $solution = []; + $index = 0; + $sumLeft = 0; + $sumRight = array_sum($input); + $valuePrevious = 0; + + foreach (array_values($input) as $value) { + $sumLeft += $valuePrevious; + $sumRight -= $value; + if ($sumLeft === $sumRight) { + $solution[] = $index; // add equilibrium index to solution + } + $valuePrevious = $value; + $index++; + } + + return $solution; } +/* + * The Booj Code Test as solved by Jeff Sheffel (github.com/jscodefix, jeff@sheffel.org) + * + * My first thought was to create an elegant and complete solution to this simple code challenge. + * Some considerations are input validation, performance, and edge cases. + * The edge cases for this problem include consideration for the processing the beginning and the end + * of the input array. It soon becomes apparent that the given definition of "an equilibrium index" does + * not consider the array edge case indicies; that is, should the sum of the non-existent elements that + * are to the left of index 0 be considered as summing to zero? In the spirit of PHP, since the non-existent + * array values can be considered as null, and adding a null values is equivalent to adding a zero integer, + * we can include the first and last indicies to our solution. + * null [0, 1, 2, 3, ... N-1, N] null + * + * Typically for array problems, coding the array indexing correctly can be tricky. + */ From c372ebec0ef6d6d3da9bc0001ce908b5fa3d3c4a Mon Sep 17 00:00:00 2001 From: Jeff Sheffel Date: Fri, 2 Jun 2017 18:20:56 -0600 Subject: [PATCH 6/7] add more phpunit tests... you know, TDD --- Test/EquilibriumIndexTest.php | 40 ++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/Test/EquilibriumIndexTest.php b/Test/EquilibriumIndexTest.php index 0147671..9c72beb 100644 --- a/Test/EquilibriumIndexTest.php +++ b/Test/EquilibriumIndexTest.php @@ -1,17 +1,47 @@ assertEquals(array(3,6), Booj\CodeTest\getEquilibriums($input)); - } + // Booj litmus test + $input = [-7, 1, 5, 2, -4, 3, 0]; + $this->assertEquals([3,6], getEquilibriums($input)); + + // special case of empty array + $input = []; + $this->assertEquals([], getEquilibriums($input)); + + // special case of [0, N] solution + $input = [0, -3, -2, -1, 1, 2, 3, 0]; + $this->assertEquals([0, 7], getEquilibriums($input)); + + // special case of null element + $input = [1, 0, -1, null]; + $this->assertEquals([3], getEquilibriums($input)); + } + + /** + * @expectedException Exception + */ + public function testComputeEquilibriumExceptions() + { + // special case of array in an array + $input = [1, 0, -1, [8, 9]]; + getEquilibriums($input); + + // special case of an array with an object + $input = [1, 0, -1, new \StdClass]; + getEquilibriums($input); + } } From 67739ed2cfb06fce06965f5d902c13981c97805b Mon Sep 17 00:00:00 2001 From: Jeff Sheffel Date: Fri, 2 Jun 2017 18:22:00 -0600 Subject: [PATCH 7/7] add exception for invalid array values; add code comments --- EquilibriumIndex.php | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/EquilibriumIndex.php b/EquilibriumIndex.php index 6c1ef51..69d7302 100644 --- a/EquilibriumIndex.php +++ b/EquilibriumIndex.php @@ -3,10 +3,15 @@ namespace Booj\CodeTest; /** - * This function ... + * This function returns a solution array of "equilibrium indexes" for the input array. * - * @param $input array to + * An equilibrium index is described in the Booj codetest README.md file. + * The solution array that this function returns can additionally contain the 0 and N-1 array indicies + * (where N is the size of the input array). See comments below the function for additional details. + * + * @param $input array to solve * @return array + * @throws \Exception */ function getEquilibriums(array $input) { $solution = []; @@ -16,6 +21,10 @@ function getEquilibriums(array $input) { $valuePrevious = 0; foreach (array_values($input) as $value) { + // validate input array values; only integer and null types are allowed + if (!(is_int($value) || is_null($value))) { + throw new \Exception("Invalid array value"); + } $sumLeft += $valuePrevious; $sumRight -= $value; if ($sumLeft === $sumRight) { @@ -27,19 +36,27 @@ function getEquilibriums(array $input) { return $solution; } - + /* * The Booj Code Test as solved by Jeff Sheffel (github.com/jscodefix, jeff@sheffel.org) * * My first thought was to create an elegant and complete solution to this simple code challenge. * Some considerations are input validation, performance, and edge cases. + * * The edge cases for this problem include consideration for the processing the beginning and the end * of the input array. It soon becomes apparent that the given definition of "an equilibrium index" does * not consider the array edge case indicies; that is, should the sum of the non-existent elements that - * are to the left of index 0 be considered as summing to zero? In the spirit of PHP, since the non-existent - * array values can be considered as null, and adding a null values is equivalent to adding a zero integer, - * we can include the first and last indicies to our solution. + * are to the left of index 0 be considered as summing to zero? + * + * In the spirit of PHP, since the non-existent array values can be considered as null, and adding a + * null value is equivalent to adding a zero integer, we can include the first and last indicies to + * our solution. Visualize the array as something like: * null [0, 1, 2, 3, ... N-1, N] null + * The elegance of this approach is, that the solution extends the usual or expected equilibrium solution; + * in other words, it extends the solution that never includes the 0 or N-1 indicies. + * + * Typically for array coding problems, correctly coding the array indexing can be tricky. I decided to + * use a foreach() loop, instead of a for() loop. * - * Typically for array problems, coding the array indexing correctly can be tricky. + * This code was tested against PHP 7.0.18 (cli) and phpunit 6.1 on Ubuntu Linux. */