-
-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make "removeDuplicatedCharacters" PSR-12 friendly and add unit tests
- Loading branch information
1 parent
f406fa3
commit 37d62f9
Showing
3 changed files
with
86 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* The function loops through each character in the input string. | ||
* It uses an array $seen to keep track of characters that have already been added to the output string. | ||
* If a character hasn't been seen, it is appended to the result string, and the character is marked as seen. | ||
* The function returns the modified string with duplicate characters removed. | ||
* Removes duplicate characters from a string, retaining only the first occurrence of each character. | ||
* | ||
* @param string $inputString The input string from which duplicates will be removed. | ||
* @return string The modified string with duplicate characters removed. | ||
*/ | ||
|
||
function removeDuplicatedCharacters($inputString) { | ||
function removeDuplicatedCharacters(string $inputString): string | ||
{ | ||
// Initialize an empty array to keep track of seen characters | ||
$seen = []; | ||
|
||
// Initialize an empty string for the result | ||
$result = ''; | ||
|
||
// Loop through each character in the input string | ||
for ($i = 0; $i < strlen($inputString); $i++) { | ||
$char = $inputString[$i]; | ||
|
||
// Check if the character has already been seen | ||
if (!in_array($char, $seen)) { | ||
if (!in_array($char, $seen, true)) { | ||
// Add the character to the result and mark it as seen | ||
$result .= $char; | ||
$seen[] = $char; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require_once __DIR__ . '/../../vendor/autoload.php'; | ||
require_once __DIR__ . '/../../Strings/RemoveDuplicatedCharacters.php'; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Unit tests for the removeDuplicatedCharacters function. | ||
* To run test: ./vendor/bin/phpunit tests/strings/RemoveDuplicatedCharactersTest.php | ||
*/ | ||
final class RemoveDuplicatedCharactersTest extends TestCase | ||
{ | ||
/** | ||
* Test with a string that has multiple duplicates. | ||
*/ | ||
public function testStringWithDuplicates(): void | ||
{ | ||
$this->assertSame('progamin', removeDuplicatedCharacters('programming')); | ||
} | ||
|
||
/** | ||
* Test with a string that has no duplicates. | ||
*/ | ||
public function testStringWithoutDuplicates(): void | ||
{ | ||
$this->assertSame('abc', removeDuplicatedCharacters('abc')); | ||
} | ||
|
||
/** | ||
* Test with an empty string. | ||
*/ | ||
public function testEmptyString(): void | ||
{ | ||
$this->assertSame('', removeDuplicatedCharacters('')); | ||
} | ||
|
||
/** | ||
* Test with a string containing only one character repeated. | ||
*/ | ||
public function testSingleCharacterRepeated(): void | ||
{ | ||
$this->assertSame('a', removeDuplicatedCharacters('aaaaa')); | ||
} | ||
|
||
/** | ||
* Test with special characters. | ||
*/ | ||
public function testStringWithSpecialCharacters(): void | ||
{ | ||
$this->assertSame('ab!@', removeDuplicatedCharacters('aabb!!@@')); | ||
} | ||
|
||
/** | ||
* Test with a string containing spaces. | ||
*/ | ||
public function testStringWithSpaces(): void | ||
{ | ||
$this->assertSame('a b', removeDuplicatedCharacters('a a a b b')); | ||
} | ||
|
||
/** | ||
* Test with a string containing mixed case characters. | ||
*/ | ||
public function testStringWithMixedCase(): void | ||
{ | ||
$this->assertSame('aAbB', removeDuplicatedCharacters('aAaAaAbBbB')); | ||
} | ||
} |