function dividedByThree(int $n): bool { return base_convert(abs($n), 10, 3)[-1] == 0 && $n; }
function dividedByThree(int $number): bool- function dividedByThree(int $n): bool
- {
return $number && !($number % 3);- return base_convert(abs($n), 10, 3)[-1] == 0 && $n;
- }
class MyTestCasesTest extends TestCase { public function testDividedByThree() { $this->assertTrue(dividedByThree(3)); $this->assertTrue(dividedByThree(12)); $this->assertFalse(dividedByThree(13)); $this->assertFalse(dividedByThree(0)); // actually we got a remainder of 0 here, so it should be "assertTrue" $this->assertTrue(dividedByThree(-12)); } public function testRandomNumbers(){ $random_number_array = range(1, 100); shuffle($random_number_array ); foreach($random_number_array as $value){ $isDivisible = $value % 3 == 0; $this->assertEquals($isDivisible,dividedByThree($value)); } } }
- class MyTestCasesTest extends TestCase
- {
- public function testDividedByThree() {
- $this->assertTrue(dividedByThree(3));
- $this->assertTrue(dividedByThree(12));
- $this->assertFalse(dividedByThree(13));
$this->assertFalse(dividedByThree(0));- $this->assertFalse(dividedByThree(0)); // actually we got a remainder of 0 here, so it should be "assertTrue"
- $this->assertTrue(dividedByThree(-12));
- }
- public function testRandomNumbers(){
- $random_number_array = range(1, 100);
- shuffle($random_number_array );
- foreach($random_number_array as $value){
- $isDivisible = $value % 3 == 0;
- $this->assertEquals($isDivisible,dividedByThree($value));
- }
- }
- }
Fundamentals
Logic
function binaryGap($n) { return max(array_map('strlen', explode(1, trim(decbin($n), 0)))); }
- function binaryGap($n) {
return max(array_map('strlen', explode(1, decbin($n))));- return max(array_map('strlen', explode(1, trim(decbin($n), 0))));
- }
<?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(bindec('10000010001'))); $this->assertEquals(2, binaryGap(bindec('1001'))); $this->assertEquals(0, binaryGap(bindec('1111'))); $this->assertEquals(2, binaryGap(bindec('101001'))); $this->assertEquals(2, binaryGap(bindec('100101'))); $this->assertEquals(2, binaryGap(bindec('100110'))); $this->assertEquals(1, binaryGap(bindec('101100'))); $this->assertEquals(0, binaryGap(bindec('1000'))); $this->assertEquals(0, binaryGap(bindec('001000'))); $this->assertEquals(0, binaryGap(bindec('000100'))); $this->assertEquals(0, binaryGap(bindec('001100'))); $this->assertEquals(2, binaryGap(bindec('010010'))); $this->assertEquals(0, binaryGap(bindec('0'))); } }
- <?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(bindec('10000010001')));
- $this->assertEquals(2, binaryGap(bindec('1001')));
- $this->assertEquals(0, binaryGap(bindec('1111')));
- $this->assertEquals(2, binaryGap(bindec('101001')));
- $this->assertEquals(2, binaryGap(bindec('100101')));
- $this->assertEquals(2, binaryGap(bindec('100110')));
$this->assertEquals(2, binaryGap(bindec('101100')));- $this->assertEquals(1, binaryGap(bindec('101100')));
- $this->assertEquals(0, binaryGap(bindec('1000')));
- $this->assertEquals(0, binaryGap(bindec('001000')));
- $this->assertEquals(0, binaryGap(bindec('000100')));
- $this->assertEquals(0, binaryGap(bindec('001100')));
- $this->assertEquals(2, binaryGap(bindec('010010')));
- $this->assertEquals(0, binaryGap(bindec('0')));
- }
- }
Fundamentals
Logic
function binaryGap($n) { return max(array_map('strlen', explode(1, decbin($n)))); }
- function binaryGap($n) {
return max([...array_map('strlen', explode(1, decbin($n))), 0]);- return max(array_map('strlen', explode(1, decbin($n))));
- }
<?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(bindec('10000010001'))); $this->assertEquals(2, binaryGap(bindec('1001'))); $this->assertEquals(0, binaryGap(bindec('1111'))); $this->assertEquals(2, binaryGap(bindec('101001'))); $this->assertEquals(2, binaryGap(bindec('100101'))); $this->assertEquals(2, binaryGap(bindec('100110'))); $this->assertEquals(2, binaryGap(bindec('101100'))); } }
- <?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(bindec('10000010001')));
- $this->assertEquals(2, binaryGap(bindec('1001')));
- $this->assertEquals(0, binaryGap(bindec('1111')));
- $this->assertEquals(2, binaryGap(bindec('101001')));
- $this->assertEquals(2, binaryGap(bindec('100101')));
- $this->assertEquals(2, binaryGap(bindec('100110')));
- $this->assertEquals(2, binaryGap(bindec('101100')));
- }
- }
Fundamentals
Logic
function binaryGap($n) { return max([...array_map('strlen', explode(1, decbin($n))), 0]); }
- function binaryGap($n) {
preg_match_all('/[0]+/', trim(decbin($n), 0), $_);return $_[0] ? max(array_map('strlen', $_[0])) : 0;- return max([...array_map('strlen', explode(1, decbin($n))), 0]);
- }
<?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(bindec('10000010001'))); $this->assertEquals(2, binaryGap(bindec('1001'))); $this->assertEquals(0, binaryGap(bindec('1111'))); $this->assertEquals(2, binaryGap(bindec('101001'))); $this->assertEquals(2, binaryGap(bindec('100101'))); } }
- <?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));- $this->assertEquals(5, binaryGap(bindec('10000010001')));
- $this->assertEquals(2, binaryGap(bindec('1001')));
- $this->assertEquals(0, binaryGap(bindec('1111')));
- $this->assertEquals(2, binaryGap(bindec('101001')));
- $this->assertEquals(2, binaryGap(bindec('100101')));
- }
- }