Zend based exercise to find the index that splits a numeric array in two equal sums of values
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


== README.md == https://github.com/javierrey/exercise-equilibrium-index ==

== Equilibrium Index Exercise. 2015-04-20. By Javier Rey. javier.rey.eu@gmail.com ==


Equilibrium index: the index in an array which divides it into two arrays where the sum
of the elements are equal on the two sides.

Example: For the following array [2,1,7,3] the equilibrium index will be equal to “2”:
[2, 1] = [3]

Using best OOP practices and applicable PHP programming patterns write a ZF2 Application
which will:

• Define and implement a ZF2 service which will solve the equilibrium index problem for
  the array passed as a parameter and return a result.
• Create a route and action which will get the array elements from the URL segment, use
  defined service and pass the output data to the view.
• URL format should be: */equilibrium-index/comma,separated,array,elements[/strict]
• Use plain print_r() function in the view to output the data
• Any non-numeric array elements should be discarded.
• Write a PHP unit test for the solution


• Define and implement a ZF2 controller plugin which will implement defined service and
  use it in the controller instead of the service.
• If strict parameter is set and non-numeric array elements are provided the application
  should return 400 HTTP response code.
• Publish solution on BitBucket or Github.


Use ZF2 skeleton application as a starting point:

Refer to ZF2 documentation: http://framework.zend.com/manual/2.2/en/index.html


Project is based in Zend 2 PHP framework and contains 3 modules:
Application, EquilibriumIndex and UnitTests.

Default module, Application, holds the user interface layout and performs the request
to the core service, located in the EquilibriumIndex module, which returns an array
with the calculated equilibrium indexes, as per the requirement specification.

The result will be an array of integers, the found indexes, whose length will be
between zero and the length of the source array - 2.

Additionally, the Application's view will allow the user to call the unitTests module
service, which will perform a system call to a PHPUnit test, passing the same source
parameters and printing the returned test output.

The possible test results will be:
Success, if an equilibrium index is found.
Failure, if no index is found.
Skipped, if the source array has non-numeric values and the strict mode is on.

Note: Output from service is invoked from client Ajax, so the 'print_r' output
mentioned in the requirements is replaced here by a 'json_encode' format.


Web URL (replace hostname as appropriate):



    Array field: A comma separated list of numeric values (and optionally non-numeric).
    Strict checkbox: Which evaluates to boolean.

The Generate button creates a random set of values in the Array field, like:

    8, 6, 5, 5, 4, 7, 7

Random values in the array will range from -9 to 9, and array length between 3 and 9.

Feel free to modify the generated parameter like:

    8, 456, hello, 1, -566, 4, 7.5, -7.437, 14, 14

In the above example, if the strict checkbox is ticked, service should return a
Bad Request response header, due to the presence of non-numeric values.

If the strict box is unchecked, the use of fractional values will decrease the chances
of finding an equilibrium index, and an empty array will be returned.

Spaces and quotes are optional in the Array field and will be removed in the request.

Development summary (change paths for other development environments):

1. Download ZF2 skeleton with Composer:
    php /home/projects/php/bin/composer.phar create-project
2. Clean/Refactor default skeleton:
    2.1. Add phpunit.phar to /vendor/bin/ and move composer.phar to same folder.
    2.2. Clean languages/ folder, only contains en_GB (edited with Poedit) and move to
         module assets/ subfolder.
    2.3. Clean default layout and view.
    2.4. Simplify/Refactor /public/ folder's includes and assets.
3. Update default module config (same for newly created modules):
    3.1. Create a config.json file in the config folder for custom properties.
    3.2. Default controller named as Main (rather than Index, default route would be
    3.3. Optional route context prefix (needed for certain nested htaccess
    3.4. Translator language in /module/assets/ folder set to en_GB.
4. Create a Common folder under /module/, for shared static functionality.
5. Create core service module, EquilibriumIndex.
6. Create module UnitTests for handling system based PHPUnit tests.
    6.1. Create a PHPUnit test folder, class and method to be used by module UnitTests.

Requirements and dependencies:

This project requires Apache and PHP (v5.3) properly installed in the hosting system.

The composer.phar and phpunit.phar files are included in the /vendor/bin/ folder.

Depending on the deployment location, host name and folder names, '.htaccess' files in
the project root and the public folder might need to be updated accordingly.