Skip to content

Commit 02e377f

Browse files
authored
Sync anagram (#767)
1 parent e763150 commit 02e377f

File tree

3 files changed

+133
-80
lines changed

3 files changed

+133
-80
lines changed

exercises/practice/anagram/.meta/example.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
function detectAnagrams($anagram, array $possibleMatches)

exercises/practice/anagram/.meta/tests.toml

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[dd40c4d2-3c8b-44e5-992a-f42b393ec373]
613
description = "no matches"
714

815
[b3cca662-f50a-489e-ae10-ab8290a09bdc]
916
description = "detects two anagrams"
17+
include = false
18+
19+
[03eb9bbe-8906-4ea0-84fa-ffe711b52c8b]
20+
description = "detects two anagrams"
21+
reimplements = "b3cca662-f50a-489e-ae10-ab8290a09bdc"
1022

1123
[a27558ee-9ba0-4552-96b1-ecf665b06556]
1224
description = "does not detect anagram subsets"
@@ -34,12 +46,43 @@ description = "detects anagrams using case-insensitive possible matches"
3446

3547
[7cc195ad-e3c7-44ee-9fd2-d3c344806a2c]
3648
description = "does not detect an anagram if the original word is repeated"
49+
include = false
50+
51+
[630abb71-a94e-4715-8395-179ec1df9f91]
52+
description = "does not detect an anagram if the original word is repeated"
53+
reimplements = "7cc195ad-e3c7-44ee-9fd2-d3c344806a2c"
3754

3855
[9878a1c9-d6ea-4235-ae51-3ea2befd6842]
3956
description = "anagrams must use all letters exactly once"
4057

4158
[85757361-4535-45fd-ac0e-3810d40debc1]
4259
description = "words are not anagrams of themselves (case-insensitive)"
60+
include = false
61+
62+
[68934ed0-010b-4ef9-857a-20c9012d1ebf]
63+
description = "words are not anagrams of themselves"
64+
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1"
65+
66+
[589384f3-4c8a-4e7d-9edc-51c3e5f0c90e]
67+
description = "words are not anagrams of themselves even if letter case is partially different"
68+
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1"
69+
70+
[ba53e423-7e02-41ee-9ae2-71f91e6d18e6]
71+
description = "words are not anagrams of themselves even if letter case is completely different"
72+
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1"
4373

4474
[a0705568-628c-4b55-9798-82e4acde51ca]
4575
description = "words other than themselves can be anagrams"
76+
include = false
77+
78+
[33d3f67e-fbb9-49d3-a90e-0beb00861da7]
79+
description = "words other than themselves can be anagrams"
80+
reimplements = "a0705568-628c-4b55-9798-82e4acde51ca"
81+
82+
[a6854f66-eec1-4afd-a137-62ef2870c051]
83+
include = false
84+
description = "handles case of greek letters"
85+
86+
[fd3509e5-e3ba-409d-ac3d-a9ac84d13296]
87+
description = "different characters may have the same bytes"
88+
include = false
Lines changed: 87 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
class AnagramTest extends PHPUnit\Framework\TestCase
@@ -31,108 +9,162 @@ public static function setUpBeforeClass(): void
319
require_once 'Anagram.php';
3210
}
3311

12+
/**
13+
* uuid dd40c4d2-3c8b-44e5-992a-f42b393ec373
14+
* @testdox No matches
15+
*/
3416
public function testNoMatches(): void
3517
{
3618
$this->assertEquals([], detectAnagrams('diaper', ['hello', 'world', 'zombies', 'pants']));
3719
}
3820

39-
public function testDetectsSimpleAnagram(): void
21+
/**
22+
* uuid 03eb9bbe-8906-4ea0-84fa-ffe711b52c8b
23+
* @testdox Detects two anagrams
24+
*/
25+
public function testDetectsTwoAnagrams(): void
4026
{
41-
$this->assertEquals(['tan'], detectAnagrams('ant', ['tan', 'stand', 'at']));
42-
}
43-
44-
public function testDoesNotDetectFalsePositives(): void
45-
{
46-
$this->assertEquals([], detectAnagrams('galea', ['eagle']));
47-
}
48-
49-
public function testDetectsMultipleAnagrams(): void
50-
{
51-
$this->assertEquals(['stream', 'maters'], detectAnagrams('master', ['stream', 'pigeon', 'maters']));
27+
$this->assertEquals(
28+
['lemons', 'melons'],
29+
detectAnagrams('solemn', ['lemons', 'cherry', 'melons'])
30+
);
5231
}
5332

33+
/**
34+
* uuid a27558ee-9ba0-4552-96b1-ecf665b06556
35+
* @testdox Does not detect anagram subsets
36+
*/
5437
public function testDoesNotDetectAnagramSubsets(): void
5538
{
5639
$this->assertEquals([], detectAnagrams('good', ['dog', 'goody']));
5740
}
5841

42+
/**
43+
* uuid 64cd4584-fc15-4781-b633-3d814c4941a4
44+
* @testdox Detects anagram
45+
*/
5946
public function testDetectsAnagram(): void
6047
{
6148
$this->assertEquals(['inlets'], detectAnagrams('listen', ['enlists', 'google', 'inlets', 'banana']));
6249
}
6350

64-
public function testDetectsMultipleAnagrams2(): void
51+
/**
52+
* uuid 99c91beb-838f-4ccd-b123-935139917283
53+
* @testdox Detects three anagrams
54+
*/
55+
public function testDetectsThreeAnagrams(): void
6556
{
6657
$this->assertEquals(
6758
['gallery', 'regally', 'largely'],
6859
detectAnagrams('allergy', ['gallery', 'ballerina', 'regally', 'clergy', 'largely', 'leading'])
6960
);
7061
}
7162

72-
public function testDoesNotDetectIdenticalWords(): void
63+
/**
64+
* uuid 78487770-e258-4e1f-a646-8ece10950d90
65+
* @testdox Detects multiple anagrams with different case
66+
*/
67+
public function testDetectsMultipleAnagramsWithDifferentCase(): void
7368
{
74-
$this->assertEquals(['cron'], detectAnagrams('corn', ['corn', 'dark', 'Corn', 'rank', 'CORN', 'cron', 'park']));
69+
$this->assertEquals(['Eons', 'ONES'], detectAnagrams('nose', ['Eons', 'ONES']));
7570
}
7671

72+
/**
73+
* uuid 1d0ab8aa-362f-49b7-9902-3d0c668d557b
74+
* @testdox Does not detect non-anagrams with identical checksum
75+
*/
7776
public function testDoesNotDetectNonAnagramsWithIdenticalChecksum(): void
7877
{
7978
$this->assertEquals([], detectAnagrams('mass', ['last']));
8079
}
8180

81+
/**
82+
* uuid 9e632c0b-c0b1-4804-8cc1-e295dea6d8a8
83+
* @testdox Detects anagrams case-insensitively
84+
*/
8285
public function testDetectsAnagramsCaseInsensitively(): void
8386
{
8487
$this->assertEquals(['Carthorse'], detectAnagrams('Orchestra', ['cashregister', 'Carthorse', 'radishes']));
8588
}
8689

90+
/**
91+
* uuid b248e49f-0905-48d2-9c8d-bd02d8c3e392
92+
* @testdox Detects anagrams using case-insensitive subject
93+
*/
8794
public function testDetectsAnagramsUsingCaseInsensitiveSubject(): void
8895
{
8996
$this->assertEquals(['carthorse'], detectAnagrams('Orchestra', ['cashregister', 'carthorse', 'radishes']));
9097
}
9198

99+
/**
100+
* uuid 5c3d6a8d-7e0b-4b9e-9e1e-6c7d7f8b9c0c
101+
* @testdox Detects anagrams using case-insensitive possible matches
102+
*/
92103
public function testDetectsAnagramsUsingCaseInsensitvePossibleMatches(): void
93104
{
94105
$this->assertEquals(['Carthorse'], detectAnagrams('orchestra', ['cashregister', 'Carthorse', 'radishes']));
95106
}
96107

97-
public function testDoesNotDetectAWordAsItsOwnAnagram(): void
98-
{
99-
$this->assertEquals([], detectAnagrams('banana', ['Banana']));
100-
}
101-
108+
/**
109+
* uuid 630abb71-a94e-4715-8395-179ec1df9f91
110+
* @testdox Does not detect an anagram if the original word is repeated
111+
*/
102112
public function testDoesNotDetectAAnagramIfTheOriginalWordIsRepeated(): void
103113
{
104-
$this->assertEquals([], detectAnagrams('go', ['go Go GO']));
114+
$this->assertEquals([], detectAnagrams('go', ['goGoGO']));
105115
}
106116

117+
/**
118+
* uuid 9878a1c9-d6ea-4235-ae51-3ea2befd6842
119+
* @testdox Anagrams must use all letters exactly once
120+
*/
107121
public function testAnagramsMustUseAllLettersExactlyOnce(): void
108122
{
109123
$this->assertEquals([], detectAnagrams('tapper', ['patter']));
110124
}
111125

112-
public function testEliminatesAnagramsWithTheSameChecksum(): void
126+
/**
127+
* uuid 68934ed0-010b-4ef9-857a-20c9012d1ebf
128+
* @testdox Words are not anagrams of themselves
129+
*/
130+
public function testWordsAreNotAnagramsOfThemselves(): void
113131
{
114-
$this->assertEquals([], detectAnagrams('mass', ['last']));
132+
$this->assertEquals([], detectAnagrams('BANANA', ['BANANA']));
115133
}
116134

117-
public function testDetectsUnicodeAnagrams(): void
135+
/**
136+
* uuid 589384f3-4c8a-4e7d-9edc-51c3e5f0c90e
137+
* @testdox Words are not anagrams of themselves even if letter case is partially different
138+
*/
139+
public function testWordsAreNotAnagramsOfThemselvesEvenIfLetterCaseIsPartiallyDifferent(): void
118140
{
119-
$this->markTestSkipped('This requires `mbstring` to be installed and thus is optional.');
120-
$this->assertEquals(['ΒΓΑ', 'γβα'], detectAnagrams('ΑΒΓ', ['ΒΓΑ', 'ΒΓΔ', 'γβα']));
141+
$this->assertEquals([], detectAnagrams('BANANA', ['Banana']));
121142
}
122143

123-
public function testEliminatesMisleadingUnicodeAnagrams(): void
144+
/**
145+
* uuid ba53e423-7e02-41ee-9ae2-71f91e6d18e6
146+
* @testdox Words are not anagrams of themselves even if letter case is completely different
147+
*/
148+
public function testWordsAreNotAnagramsOfThemselvesEvenIfLetterCaseIsCompletelyDifferent(): void
124149
{
125-
$this->markTestSkipped('This requires `mbstring` to be installed and thus is optional.');
126-
$this->assertEquals([], detectAnagrams('ΑΒΓ', ['ABΓ']));
150+
$this->assertEquals([], detectAnagrams('BANANA', ['banana']));
127151
}
128152

129-
public function testCapitalWordIsNotOwnAnagram(): void
153+
/**
154+
* uuid 33d3f67e-fbb9-49d3-a90e-0beb00861da7
155+
* @testdox Words other than themselves can be anagrams
156+
*/
157+
public function testWordsOtherThanThemselvesCanBeAnagrams(): void
130158
{
131-
$this->assertEquals([], detectAnagrams('BANANA', ['Banana']));
159+
$this->assertEquals(['Silent'], detectAnagrams('LISTEN', ['LISTEN', 'Silent']));
132160
}
133161

134-
public function testAnagramsMustUseAllLettersExactlyOnce2(): void
162+
/**
163+
* uuid a6854f66-eec1-4afd-a137-62ef2870c051
164+
* @testdox Handles case of greek letters
165+
*/
166+
public function testHandlesCaseOfGreekLetters(): void
135167
{
136-
$this->assertEquals([], detectAnagrams('patter', ['tapper']));
168+
$this->markTestSkipped('This requires `mbstring` to be installed and thus is optional.');
137169
}
138170
}

0 commit comments

Comments
 (0)