Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change: sync binary search #738

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/practice/binary-search/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Binary search only works when a list has been sorted.

The algorithm looks like this:

- Find the middle element of a *sorted* list and compare it with the item we're looking for.
- Find the middle element of a _sorted_ list and compare it with the item we're looking for.
- If the middle element is our item, then we're done!
- If the middle element is greater than our item, we can eliminate that element and all the elements **after** it.
- If the middle element is less than our item, we can eliminate that element and all the elements **before** it.
Expand Down
22 changes: 0 additions & 22 deletions exercises/practice/binary-search/.meta/example.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

function find($needle, $haystack)
Expand Down
77 changes: 55 additions & 22 deletions exercises/practice/binary-search/BinarySearchTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

class BinarySearchTest extends PHPUnit\Framework\TestCase
Expand All @@ -31,47 +9,102 @@ public static function setUpBeforeClass(): void
require_once 'BinarySearch.php';
}

/**
* @testdox It works with one element
* uuid b55c24a9-a98d-4379-a08c-2adcf8ebeee8
*/
public function testItWorksWithOneElement(): void
{
$this->assertEquals(0, find(6, [6]));
}

/**
* @testdox It finds value in the middle
* uuid 73469346-b0a0-4011-89bf-989e443d503d
*/
public function testItFindsValueInMiddle(): void
{
$this->assertEquals(3, find(6, [1, 3, 4, 6, 8, 9, 11]));
}

/**
* @testdox It finds value at the beginning
* uuid 327bc482-ab85-424e-a724-fb4658e66ddb
*/
public function testItFindsValueInBeginning(): void
{
$this->assertEquals(0, find(1, [1, 3, 4, 6, 8, 9, 11]));
}

/**
* @testdox It finds value at the end
* uuid f9f94b16-fe5e-472c-85ea-c513804c7d59
*/
public function testItFindsValueAtEnd(): void
{
$this->assertEquals(6, find(11, [1, 3, 4, 6, 8, 9, 11]));
}

/**
* @testdox It finds value in an odd-length array
* uuid f0068905-26e3-4342-856d-ad153cadb338
*/
public function testItFindsValueInOddLengthArray(): void
{
$this->assertEquals(9, find(144, [1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634]));
}

/**
* @testdox It finds value in an even-length array
* uuid fc316b12-c8b3-4f5e-9e89-532b3389de8c
*/
public function testItFindsValueInEvenLengthArray(): void
{
$this->assertEquals(5, find(21, [1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]));
}

/**
* @testdox It identifies that value is not in array
* uuid da7db20a-354f-49f7-a6a1-650a54998aa6
*/
public function testValueNotIncludedInArray(): void
{
$this->assertEquals(-1, find(7, [1, 3, 4, 6, 8, 9, 11]));
}

/**
* @testdox It does not find a value lower than the lowest value
* uuid 95d869ff-3daf-4c79-b622-6e805c675f97
*/
public function testLowerThanLowestValueNotIn(): void
{
$this->assertEquals(-1, find(0, [1, 3, 4, 6, 8, 9, 11]));
}

/**
* @testdox It does not find a value larger than the largest value
* uuid 8b24ef45-6e51-4a94-9eac-c2bf38fdb0ba
*/
public function testLargerThanLargestValueNotIn(): void
{
$this->assertEquals(-1, find(13, [1, 3, 4, 6, 8, 9, 11]));
}

/**
* @testdox It does not find any value in an empty array
* uuid f439a0fa-cf42-4262-8ad1-64bf41ce566a
*/
public function testNothingInEmptyArray(): void
{
$this->assertEquals(-1, find(1, []));
}

/**
* @testdox It does not find any value in left and right of array
* uuid 2c353967-b56d-40b8-acff-ce43115eed64
*/
public function testNothingFoundInLeftAndRight()
{
$this->assertEquals(-1, find(0, [1, 2]));
}
}