Fundamentals
Logic
function binaryGap($n) { $binary = trim(decbin($n), 0); preg_match_all('/[0]+/', $binary, $match); $max = 0; foreach ($match[0] as $zeros) { $max = max($max, strlen($zeros)); } return $max; }
- function binaryGap($n) {
- $binary = trim(decbin($n), 0);
- preg_match_all('/[0]+/', $binary, $match);
- $max = 0;
- foreach ($match[0] as $zeros) {
- $max = max($max, strlen($zeros));
- }
- return $max;
- }
<?php use PHPUnit\Framework\TestCase; // PHPUnit Test Examples: // TODO: Replace examples and use TDD by writing your own tests class ExampleTest extends TestCase { // test function names should start with "test" public function testThatSomethingShouldHappen() { $this->assertEquals(5, binaryGap(1041)); $this->assertEquals(2, binaryGap(9)); $this->assertEquals(0, binaryGap(15)); } }
- <?php
- use PHPUnit\Framework\TestCase;
- // PHPUnit Test Examples:
- // TODO: Replace examples and use TDD by writing your own tests
- class ExampleTest extends TestCase
- {
- // test function names should start with "test"
- public function testThatSomethingShouldHappen() {
- $this->assertEquals(5, binaryGap(1041));
$this->assertEquals(9, binaryGap(9));- $this->assertEquals(2, binaryGap(9));
- $this->assertEquals(0, binaryGap(15));
- }
- }