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

#48in24: anagram #767

Merged
merged 5 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 0 additions & 22 deletions exercises/practice/anagram/.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 detectAnagrams($anagram, array $possibleMatches)
Expand Down
48 changes: 45 additions & 3 deletions exercises/practice/anagram/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

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

[b3cca662-f50a-489e-ae10-ab8290a09bdc]
description = "detects two anagrams"
include = false

[03eb9bbe-8906-4ea0-84fa-ffe711b52c8b]
description = "detects two anagrams"
reimplements = "b3cca662-f50a-489e-ae10-ab8290a09bdc"

[a27558ee-9ba0-4552-96b1-ecf665b06556]
description = "does not detect anagram subsets"
Expand Down Expand Up @@ -34,12 +46,42 @@ description = "detects anagrams using case-insensitive possible matches"

[7cc195ad-e3c7-44ee-9fd2-d3c344806a2c]
description = "does not detect an anagram if the original word is repeated"
include = false

[630abb71-a94e-4715-8395-179ec1df9f91]
description = "does not detect an anagram if the original word is repeated"
reimplements = "7cc195ad-e3c7-44ee-9fd2-d3c344806a2c"

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

[85757361-4535-45fd-ac0e-3810d40debc1]
description = "words are not anagrams of themselves (case-insensitive)"
include = false

[68934ed0-010b-4ef9-857a-20c9012d1ebf]
description = "words are not anagrams of themselves"
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1"

[589384f3-4c8a-4e7d-9edc-51c3e5f0c90e]
description = "words are not anagrams of themselves even if letter case is partially different"
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1"

[ba53e423-7e02-41ee-9ae2-71f91e6d18e6]
description = "words are not anagrams of themselves even if letter case is completely different"
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1"

[a0705568-628c-4b55-9798-82e4acde51ca]
description = "words other than themselves can be anagrams"
include = false

[33d3f67e-fbb9-49d3-a90e-0beb00861da7]
description = "words other than themselves can be anagrams"
reimplements = "a0705568-628c-4b55-9798-82e4acde51ca"

[a6854f66-eec1-4afd-a137-62ef2870c051]
description = "handles case of greek letters"

[fd3509e5-e3ba-409d-ac3d-a9ac84d13296]
description = "different characters may have the same bytes"
include = false
148 changes: 90 additions & 58 deletions exercises/practice/anagram/AnagramTest.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 AnagramTest extends PHPUnit\Framework\TestCase
Expand All @@ -31,108 +9,162 @@ public static function setUpBeforeClass(): void
require_once 'Anagram.php';
}

/**
* @testdox no matches
* uuid dd40c4d2-3c8b-44e5-992a-f42b393ec373
fejan-malek marked this conversation as resolved.
Show resolved Hide resolved
*/
public function testNoMatches(): void
{
$this->assertEquals([], detectAnagrams('diaper', ['hello', 'world', 'zombies', 'pants']));
}

public function testDetectsSimpleAnagram(): void
{
$this->assertEquals(['tan'], detectAnagrams('ant', ['tan', 'stand', 'at']));
}

public function testDoesNotDetectFalsePositives(): void
/**
* @testdox detects two anagrams
* uuid 03eb9bbe-8906-4ea0-84fa-ffe711b52c8b
*/
public function testDetectsTwoAnagrams(): void
{
$this->assertEquals([], detectAnagrams('galea', ['eagle']));
}

public function testDetectsMultipleAnagrams(): void
{
$this->assertEquals(['stream', 'maters'], detectAnagrams('master', ['stream', 'pigeon', 'maters']));
$this->assertEquals(
["lemons", "melons"],
fejan-malek marked this conversation as resolved.
Show resolved Hide resolved
detectAnagrams('solemn', ["lemons", "cherry", "melons"])
);
}

/**
* @testdox does not detect anagram subsets
* uuid a27558ee-9ba0-4552-96b1-ecf665b06556
*/
public function testDoesNotDetectAnagramSubsets(): void
{
$this->assertEquals([], detectAnagrams('good', ['dog', 'goody']));
}

/**
* @testdox detects anagram
* uuid 64cd4584-fc15-4781-b633-3d814c4941a4
*/
public function testDetectsAnagram(): void
{
$this->assertEquals(['inlets'], detectAnagrams('listen', ['enlists', 'google', 'inlets', 'banana']));
}

public function testDetectsMultipleAnagrams2(): void
/**
* @testdox detects three anagrams
* uuid 99c91beb-838f-4ccd-b123-935139917283
*/
public function testDetectsThreeAnagrams(): void
{
$this->assertEquals(
['gallery', 'regally', 'largely'],
detectAnagrams('allergy', ['gallery', 'ballerina', 'regally', 'clergy', 'largely', 'leading'])
);
}

public function testDoesNotDetectIdenticalWords(): void
/**
* @testdox detects multiple anagrams with different case
* uuid 78487770-e258-4e1f-a646-8ece10950d90
*/
public function testDetectsMultipleAnagramsWithDifferentCase(): void
{
$this->assertEquals(['cron'], detectAnagrams('corn', ['corn', 'dark', 'Corn', 'rank', 'CORN', 'cron', 'park']));
$this->assertEquals(['Eons', 'ONES'], detectAnagrams('nose', ['Eons', 'ONES']));
}

/**
* @testdox does not detect non-anagrams with identical checksum
* uuid 1d0ab8aa-362f-49b7-9902-3d0c668d557b
*/
public function testDoesNotDetectNonAnagramsWithIdenticalChecksum(): void
{
$this->assertEquals([], detectAnagrams('mass', ['last']));
}

/**
* @testdox detects anagrams case-insensitively
* uuid 9e632c0b-c0b1-4804-8cc1-e295dea6d8a8
*/
public function testDetectsAnagramsCaseInsensitively(): void
{
$this->assertEquals(['Carthorse'], detectAnagrams('Orchestra', ['cashregister', 'Carthorse', 'radishes']));
$this->assertEquals(['Carthorse'], detectAnagrams('Orchestra', ["cashregister", "Carthorse", "radishes"]));
}

/**
* @testdox detects anagrams using case-insensitive subject
* uuid b248e49f-0905-48d2-9c8d-bd02d8c3e392
*/
public function testDetectsAnagramsUsingCaseInsensitiveSubject(): void
{
$this->assertEquals(['carthorse'], detectAnagrams('Orchestra', ['cashregister', 'carthorse', 'radishes']));
$this->assertEquals(['carthorse'], detectAnagrams('Orchestra', ["cashregister", "carthorse", "radishes"]));
}

/**
* @testdox detects anagrams using case-insensitive possible matches
* uuid 5c3d6a8d-7e0b-4b9e-9e1e-6c7d7f8b9c0c
*/
public function testDetectsAnagramsUsingCaseInsensitvePossibleMatches(): void
{
$this->assertEquals(['Carthorse'], detectAnagrams('orchestra', ['cashregister', 'Carthorse', 'radishes']));
}

public function testDoesNotDetectAWordAsItsOwnAnagram(): void
{
$this->assertEquals([], detectAnagrams('banana', ['Banana']));
$this->assertEquals(['Carthorse'], detectAnagrams('orchestra', ["cashregister", "Carthorse", "radishes"]));
}

/**
* @testdox does not detect an anagram if the original word was not an anagram
fejan-malek marked this conversation as resolved.
Show resolved Hide resolved
* uuid 630abb71-a94e-4715-8395-179ec1df9f91
*/
public function testDoesNotDetectAAnagramIfTheOriginalWordIsRepeated(): void
{
$this->assertEquals([], detectAnagrams('go', ['go Go GO']));
$this->assertEquals([], detectAnagrams('go', ['goGoGO']));
}

/**
* @testdox anagrams must use all letters exactly once
* uuid 9878a1c9-d6ea-4235-ae51-3ea2befd6842
*/
public function testAnagramsMustUseAllLettersExactlyOnce(): void
{
$this->assertEquals([], detectAnagrams('tapper', ['patter']));
}

public function testEliminatesAnagramsWithTheSameChecksum(): void
/**
* @testdox words are not anagrams of themselves
* uuid 68934ed0-010b-4ef9-857a-20c9012d1ebf
*/
public function testWordsAreNotAnagramsOfThemselves(): void
{
$this->assertEquals([], detectAnagrams('mass', ['last']));
$this->assertEquals([], detectAnagrams('BANANA', ['BANANA']));
}

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

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

public function testCapitalWordIsNotOwnAnagram(): void
/**
* @testdox words other than themselves can be anagrams
* uuid 33d3f67e-fbb9-49d3-a90e-0beb00861da7
*/
public function testWordsOtherThanThemselvesCanBeAnagrams(): void
{
$this->assertEquals([], detectAnagrams('BANANA', ['Banana']));
$this->assertEquals(['Silent'], detectAnagrams('LISTEN', ["LISTEN", "Silent"]));
}

public function testAnagramsMustUseAllLettersExactlyOnce2(): void
/**
* @testdox handles case of greek letters
* uuid a6854f66-eec1-4afd-a137-62ef2870c051
*/
public function testHandlesCaseOfGreekLetters(): void
{
$this->assertEquals([], detectAnagrams('patter', ['tapper']));
$this->assertEquals(["ΒΓΑ", "γβα"], detectAnagrams('ΑΒΓ', ["ΒΓΑ", "ΒΓΔ", "γβα", "αβγ"]));
}
fejan-malek marked this conversation as resolved.
Show resolved Hide resolved
}