Skip to content

Commit 15d97fe

Browse files
committed
feat(Search): add naive string search and test
1 parent f4bc5d9 commit 15d97fe

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

exercises/Search/Complete/SearchComplete.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use function count;
88
use function intdiv;
9+
use function strlen;
910

1011
final class SearchComplete
1112
{
@@ -47,4 +48,22 @@ public static function binary(array $input, int $n): ?int
4748

4849
return $input[$middle] === $n ? $middle : null;
4950
}
51+
52+
public static function naive(string $input, string $n): int
53+
{
54+
$counter = 0;
55+
56+
for ($i = 0, $iMax = strlen($input), $nMax = strlen($n); $i < $iMax; ++$i) {
57+
for ($j = 0; $j < $nMax; ++$j) {
58+
if ($n[$j] !== $input[$i + $j]) {
59+
break;
60+
}
61+
if ($j === $nMax - 1) {
62+
++$counter;
63+
}
64+
}
65+
}
66+
67+
return $counter;
68+
}
5069
}

exercises/Search/Search.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
/**
88
* Implement Linear and Binary search that returns $n if found otherwise null.
9+
* Implement Naive search that returns int repetitions of $n inside of a $string.
910
*
1011
* @method static int|null linear(int[] $input, int $n)
1112
* @method static int|null binary(int[] $input, int $n)
13+
* @method static int naive(string $input, string $n)
1214
*/
1315
final class Search
1416
{

tests/Search/Complete/SearchCompleteTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ public function testLinear(): void
3030
);
3131
self::assertNull(
3232
SearchComplete::linear([34, 51, 1, 2, 3, 45, 56, 687], 100)
33-
);self::assertNull(
34-
SearchComplete::linear([], 100)
3533
);
34+
self::assertNull(SearchComplete::linear([], 100));
3635
}
3736

3837
public function testBinary(): void
@@ -43,8 +42,13 @@ public function testBinary(): void
4342
);
4443
self::assertNull(
4544
SearchComplete::binary([34, 51, 1, 2, 3, 45, 56, 687], 100)
46-
);self::assertNull(
47-
SearchComplete::binary([], 100)
4845
);
46+
self::assertNull(SearchComplete::binary([], 100));
47+
}
48+
49+
public function testNaive(): void
50+
{
51+
self::assertSame(3, SearchComplete::naive('ox mox pox', 'ox'));
52+
self::assertSame(0, SearchComplete::naive('ox mox pox', 'box'));
4953
}
5054
}

tests/Search/SearchTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@ public function testLinear(): void
2727
{
2828
self::markTestSkipped();
2929
self::assertSame(5, Search::linear([34, 51, 1, 2, 3, 45, 56, 687], 45));
30-
self::assertNull(
31-
Search::linear([34, 51, 1, 2, 3, 45, 56, 687], 100)
32-
);
30+
self::assertNull(Search::linear([34, 51, 1, 2, 3, 45, 56, 687], 100));
3331
self::assertNull(Search::linear([], 100));
3432
}
3533

3634
public function testBinary(): void
3735
{
3836
self::markTestSkipped();
3937
self::assertSame(5, Search::binary([34, 51, 1, 2, 3, 45, 56, 687], 45));
40-
self::assertNull(
41-
Search::binary([34, 51, 1, 2, 3, 45, 56, 687], 100)
42-
);
38+
self::assertNull(Search::binary([34, 51, 1, 2, 3, 45, 56, 687], 100));
4339
self::assertNull(SearchComplete::binary([], 100));
4440
}
41+
42+
public function testNaive(): void
43+
{
44+
self::markTestSkipped();
45+
self::assertSame(3, Search::naive('ox mox pox', 'ox'));
46+
self::assertSame(0, Search::naive('ox mox pox', 'box'));
47+
}
4548
}

0 commit comments

Comments
 (0)