Codility demo test (PHP)
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 | |
function equi($A) { | |
$lower_elements_sum = 0; | |
$higher_elements_sum = array_sum($A); | |
for ($i = 0, $cnt = count($A); $i < $cnt; $i++) { | |
if (isset($A[$i - 1])) { | |
$lower_elements_sum += $A[$i - 1]; | |
} | |
$higher_elements_sum -= $A[$i]; | |
if ($lower_elements_sum == $higher_elements_sum) { | |
return $i; | |
} | |
} | |
return -1; | |
} | |
class EquiTest extends PHPUnit_Framework_TestCase { | |
/** | |
* @dataProvider provider | |
*/ | |
public function testEqui($A, $acceptable_results) { | |
$this->assertTrue(in_array(equi($A), $acceptable_results)); | |
} | |
public function provider() { | |
return array( | |
array(array(-7, 1, 5, 2, -4, 3, 0), array(3, 6)), | |
array(array(3, 2, 1, -6, 0), array(4)), | |
array(array(0, 3, 2, 1, -6), array(0)), | |
array(array(1, 2, 3, 4, 5, 6), array(-1)) | |
); | |
} | |
} |
You need to have PHPUnit installed. https://stackoverflow.com/questions/6065730/why-fatal-error-class-phpunit-framework-testcase-not-found-in
Also, since phpunit ver 6, it should be \PHPUnit\Framework\TestCase
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"Fatal error: Class 'PHPUnit_Framework_TestCase' not found in C:\xampp\htdocs\array\index.php on line 18"
How to define PHPUnit_Framework_TestCase ?