Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
composer.lock
.idea/
65 changes: 59 additions & 6 deletions EquilibriumIndex.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
<?php

# Put a valid docblock here!
function getEquilibriums($arr) {
$output = array();
# Logic goes here!
return $output;
namespace Booj\CodeTest;

/**
* This function returns a solution array of "equilibrium indexes" for the input array.
*
* 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 = [];
$index = 0;
$sumLeft = 0;
$sumRight = array_sum($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) {
$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 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.
*
* This code was tested against PHP 7.0.18 (cli) and phpunit 6.1 on Ubuntu Linux.
*/
12 changes: 0 additions & 12 deletions EquilibriumIndexTest.php

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
47 changes: 47 additions & 0 deletions Test/EquilibriumIndexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Booj\CodeTest;

require_once("EquilibriumIndex.php");

use PHPUnit\Framework\Exception;
use PHPUnit\Framework\TestCase;

class EquilibriumIndexTest extends TestCase
{
/**
*
*/
public function testComputeEquilibrium()
{
// 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);
}
}
16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Booj PHP">
<directory suffix="Test.php">./Test</directory>
</testsuite>
</testsuites>
<filter>
<blacklist>
<directory>./vendor</directory>
</blacklist>
</filter>
</phpunit>