Refactored for acepting any number basis.
function getIDS(string $number) { $number = str_split($number); array_walk($number, function($digit) use ($number) { return intval($digit); }); return array_sum($number); }
function getIDS($number) {return array_sum(str_split($number));- function getIDS(string $number) {
- $number = str_split($number);
- array_walk($number, function($digit) use ($number) {
- return intval($digit);
- });
- return array_sum($number);
- }
class MyTestCases extends TestCase { public function testOutput() { $this->assertEquals(getIDS('1345'), 13); $this->assertEquals(getIDS('12'), 3); $this->assertEquals(getIDS('110'), 2); $this->assertEquals(getIDS('011'), 2); $this->assertEquals(getIDS('0011'), 2); $this->assertEquals(getIDS('0x011'), 2); $this->assertEquals(getIDS('0x0011'), 2); } }
- class MyTestCases extends TestCase
- {
- public function testOutput() {
- $this->assertEquals(getIDS('1345'), 13);
- $this->assertEquals(getIDS('12'), 3);
- $this->assertEquals(getIDS('110'), 2);
- $this->assertEquals(getIDS('011'), 2);
- $this->assertEquals(getIDS('0011'), 2);
- $this->assertEquals(getIDS('0x011'), 2);
- $this->assertEquals(getIDS('0x0011'), 2);
- }
- }
With parameter int 011, the tests fail.
class MyTestCases extends TestCase { public function testOutput() { $this->assertEquals(getIDS(1345), 13); $this->assertEquals(getIDS(12), 3); $this->assertEquals(getIDS(110), 2); $this->assertEquals(getIDS(011), 2); } }
// PHPUnit Test Examples:// TODO: Replace examples and use TDD development by writing your own tests- class MyTestCases extends TestCase
- {
// test function names should start with "test"- public function testOutput() {
- $this->assertEquals(getIDS(1345), 13);
- $this->assertEquals(getIDS(12), 3);
- $this->assertEquals(getIDS(110), 2);
- $this->assertEquals(getIDS(011), 2);
- }
- }