mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-07-07 18:22:27 +02:00
add tests/Strings, change String to Strings, code style updates
This commit is contained in:
102
DIRECTORY.md
102
DIRECTORY.md
@ -1,10 +1,10 @@
|
||||
# List of all files
|
||||
|
||||
## Ciphers
|
||||
* [Caesarcipher](./ciphers/caesarCipher.php)
|
||||
* [Monoalphabeticcipher](./ciphers/monoAlphabeticCipher.php)
|
||||
* [Morsecode](./ciphers/morseCode.php)
|
||||
* [Xorcipher](./ciphers/XORCipher.php)
|
||||
* [Caesarcipher](./Ciphers/CaesarCipher.php)
|
||||
* [Monoalphabeticcipher](./Ciphers/monoAlphabeticCipher.php)
|
||||
* [Morsecode](./Ciphers/MorseCode.php)
|
||||
* [Xorcipher](./Ciphers/XORCipher.php)
|
||||
|
||||
## Conversions
|
||||
* [Binarytodecimal](./Conversions/BinaryToDecimal.php)
|
||||
@ -21,58 +21,64 @@
|
||||
* [Fibonacci](./Maths/Fibonacci.php)
|
||||
* [Fibonacci2](./Maths/Fibonacci2.php)
|
||||
* [Perfectsquare](./Maths/PerfectSquare.php)
|
||||
|
||||
## Projecteuler
|
||||
* [Problem1](./ProjectEuler/Problem1.php)
|
||||
* [Problem2](./ProjectEuler/Problem2.php)
|
||||
* [Problem3](./ProjectEuler/Problem3.php)
|
||||
* [Problem5](./ProjectEuler/Problem5.php)
|
||||
* [Problem6](./ProjectEuler/Problem6.php)
|
||||
* [Problem7](./ProjectEuler/Problem7.php)
|
||||
* [Problem9](./ProjectEuler/Problem9.php)
|
||||
* Projecteuler
|
||||
* [Problem1](./Maths/ProjectEuler/Problem1.php)
|
||||
* [Problem2](./Maths/ProjectEuler/Problem2.php)
|
||||
* [Problem3](./Maths/ProjectEuler/Problem3.php)
|
||||
* [Problem5](./Maths/ProjectEuler/Problem5.php)
|
||||
* [Problem6](./Maths/ProjectEuler/Problem6.php)
|
||||
* [Problem7](./Maths/ProjectEuler/Problem7.php)
|
||||
* [Problem9](./Maths/ProjectEuler/Problem9.php)
|
||||
|
||||
## Searches
|
||||
* [Binary Search](./searches/binary_search.php)
|
||||
* [Binarysearch](./searches/BinarySearch.php)
|
||||
* [Fibonaccisearch](./searches/fibonacciSearch.php)
|
||||
* [Linear Search](./searches/linear_search.php)
|
||||
* [Lower Bound](./searches/lower_bound.php)
|
||||
* [Upper Bound](./searches/upper_bound.php)
|
||||
* [Linearsearch](./searches/LinearSearch.php)
|
||||
* [Lowerbound](./searches/LowerBound.php)
|
||||
* [Upperbound](./searches/UpperBound.php)
|
||||
|
||||
## Sorting
|
||||
* [Arraykeyssort](./sorting/arrayKeysSort.php)
|
||||
* [Bubblesort](./sorting/bubbleSort.php)
|
||||
* [Bubblesort2](./sorting/bubbleSort2.php)
|
||||
* [Countsort](./sorting/countSort.php)
|
||||
* [Insertionsort](./sorting/insertionSort.php)
|
||||
* [Mergesort](./sorting/mergeSort.php)
|
||||
* [Quicksort](./sorting/quickSort.php)
|
||||
* [Radixsort](./sorting/radixSort.php)
|
||||
* [Selectionsort](./sorting/selectionSort.php)
|
||||
* [Bubblesort](./Sorting/BubbleSort.php)
|
||||
* [Bubblesort2](./Sorting/BubbleSort2.php)
|
||||
* [Countsort](./Sorting/CountSort.php)
|
||||
* [Insertionsort](./Sorting/InsertionSort.php)
|
||||
* [Mergesort](./Sorting/MergeSort.php)
|
||||
* [Quicksort](./Sorting/QuickSort.php)
|
||||
* [Radixsort](./Sorting/RadixSort.php)
|
||||
* [Selectionsort](./Sorting/SelectionSort.php)
|
||||
|
||||
## String
|
||||
* [Checkanagram](./String/CheckAnagram.php)
|
||||
* [Checkpalindrome](./String/CheckPalindrome.php)
|
||||
* [Checkpalindromestring](./String/CheckPalindromeString.php)
|
||||
* [Countsentences](./String/CountSentences.php)
|
||||
* [Countvowels](./String/CountVowels.php)
|
||||
* [Countvowelsconsonantnumber](./String/CountVowelsConsonantNumber.php)
|
||||
* [Editdistance](./String/EditDistance.php)
|
||||
* [Maxcharacter](./String/MaxCharacter.php)
|
||||
* [Reversestring](./String/ReverseString.php)
|
||||
* [Reversewords](./String/ReverseWords.php)
|
||||
## Strings
|
||||
* [Checkanagram](./Strings/CheckAnagram.php)
|
||||
* [Checkpalindrome](./Strings/CheckPalindrome.php)
|
||||
* [Checkpalindrome2](./Strings/CheckPalindrome2.php)
|
||||
* [Countconsonants](./Strings/CountConsonants.php)
|
||||
* [Countsentences](./Strings/CountSentences.php)
|
||||
* [Countvowels](./Strings/CountVowels.php)
|
||||
* [Distance](./Strings/Distance.php)
|
||||
* [Maxcharacter](./Strings/MaxCharacter.php)
|
||||
* [Reversestring](./Strings/ReverseString.php)
|
||||
* [Reversewords](./Strings/ReverseWords.php)
|
||||
|
||||
## Tests
|
||||
* [Cipherstest](./tests/CiphersTest.php)
|
||||
* [Ciphertest](./tests/CipherTest.php)
|
||||
* [Conversionstest](./tests/ConversionsTest.php)
|
||||
* [Decimaltobinarytest](./tests/DecimalToBinaryTest.php)
|
||||
* [Fibonaccisearchtest](./tests/fibonacciSearchTest.php)
|
||||
* [Mathtest](./tests/MathTest.php)
|
||||
* [Monoalphabeticciphertest](./tests/MonoAlphabeticCipherTest.php)
|
||||
* [Morsecodetest](./tests/MorseCodeTest.php)
|
||||
* [Projecteulertest](./tests/ProjectEulerTest.php)
|
||||
* Ciphers
|
||||
* [Cipherstest](./tests/Ciphers/CiphersTest.php)
|
||||
* [Monoalphabeticciphertest](./tests/Ciphers/MonoAlphabeticCipherTest.php)
|
||||
* [Morsecodetest](./tests/Ciphers/MorseCodeTest.php)
|
||||
* Conversions
|
||||
* [Conversionstest](./tests/Conversions/ConversionsTest.php)
|
||||
* Maths
|
||||
* [Mathstest](./tests/Maths/MathsTest.php)
|
||||
* [Projecteulertest](./tests/Maths/ProjectEulerTest.php)
|
||||
* Searches
|
||||
* [Binarysearchtest](./tests/Searches/BinarySearchTest.php)
|
||||
* [Searchestest](./tests/Searches/SearchesTest.php)
|
||||
* Sorting
|
||||
* [Arraykeyssorttest](./tests/sorting/arrayKeysSortTest.php)
|
||||
* [Countsorttest](./tests/sorting/countSortTest.php)
|
||||
* [Quicksorttest](./tests/sorting/quickSortTest.php)
|
||||
* [Stringtest](./tests/StringTest.php)
|
||||
* [Sortingtests](./tests/Sorting/SortingTests.php)
|
||||
* Strings
|
||||
* [Stringstest](./tests/Strings/StringsTest.php)
|
||||
|
||||
## Utils
|
||||
* [Arrayhelpers](./Utils/ArrayHelpers.php)
|
||||
* [Executiontime](./Utils/ExecutionTime.php)
|
||||
|
@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This is a simple way to count sentence
|
||||
* using php preg_match_all() function
|
||||
* @param string $sentence
|
||||
*/
|
||||
|
||||
function countSentences(string $sentence): int
|
||||
{
|
||||
$sentence = trim($sentence);
|
||||
|
||||
return preg_match_all('/[^\s|^\...](\.|\!|\?)(?!\w)/', $sentence);
|
||||
}
|
||||
|
||||
|
||||
// example
|
||||
echo countSentences("Hi! Hello world."); // returns 2
|
||||
echo PHP_EOL;
|
||||
echo countSentences("i am testing. test...."); // returns 2
|
||||
echo PHP_EOL;
|
||||
echo countSentences("How are you?"); // returns 1
|
||||
echo PHP_EOL;
|
@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This a simple function returns
|
||||
* the Total number of vowels total
|
||||
* number of consonant and total
|
||||
* numeric number present in the
|
||||
* given string using a simple
|
||||
* method of looping through all
|
||||
* characters present in the string
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
*/
|
||||
function countVowelsConsonant(string $string): String
|
||||
{
|
||||
if (empty($string)) {
|
||||
throw new \Exception('Please pass a non-empty string value');
|
||||
}
|
||||
|
||||
$vowels = ['a', 'e', 'i', 'o', 'u']; // Vowels Set
|
||||
$string = strtolower($string); // For case-insensitive checking
|
||||
|
||||
$vowelCount = 0;
|
||||
$consonantCount = 0;
|
||||
$numberCount = 0;
|
||||
|
||||
for ($i = 0; $i < strlen($string); $i++) {
|
||||
|
||||
if (in_array($string[$i], $vowels)) {
|
||||
|
||||
$vowelCount++;
|
||||
} elseif ($string[$i] >= 'a' && $string[$i] <= 'z') {
|
||||
|
||||
$consonantCount++;
|
||||
} elseif ($string[$i] >= 0 && $string[$i] <= 9 && is_numeric($string[$i])) {
|
||||
|
||||
$numberCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return 'total vowels - ' . $vowelCount . PHP_EOL .
|
||||
'total consonant - ' . $consonantCount . PHP_EOL .
|
||||
'total numeric number -' . $numberCount . PHP_EOL;
|
||||
}
|
||||
|
||||
// example
|
||||
echo countVowelsConsonant('Hello World. 12345');
|
||||
echo countVowelsConsonant('Count Vowels, Consonant and numeric number. 09876');
|
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
function reverse_words(string $text)
|
||||
{
|
||||
$text = trim($text);
|
||||
$words = explode(' ', $text);
|
||||
$reversedWords = [];
|
||||
for ($i = (count($words) - 1); $i >= 0; $i--) {
|
||||
$reversedWords[] = $words[$i];
|
||||
}
|
||||
return implode(' ', $reversedWords);
|
||||
}
|
@ -1,15 +1,23 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Checks if two strings are anagrams of each other.
|
||||
*
|
||||
* @param string $originalString
|
||||
* @param string $testString
|
||||
* @param bool $caseInsensitive
|
||||
* @return bool
|
||||
*/
|
||||
function isAnagram(string $originalString, string $testString, bool $caseInsensitive = true)
|
||||
{
|
||||
if ($caseInsensitive)
|
||||
if ($caseInsensitive)
|
||||
{
|
||||
$originalString = strtolower($originalString); // Converting string to lowercase for case-insensitive check
|
||||
$testString = strtolower($testString);
|
||||
}
|
||||
|
||||
//count_chars(string, mode = 1) returns key-value pairs with character as key, frequency as value
|
||||
//We can directly compare the arrays in this case
|
||||
// count_chars(string, mode = 1) returns key-value pairs with character as key, frequency as value
|
||||
// We can directly compare the arrays in this case
|
||||
if (count_chars($originalString, 1) == count_chars($testString, 1))
|
||||
{
|
||||
return true;
|
||||
@ -18,4 +26,4 @@ function isAnagram(string $originalString, string $testString, bool $caseInsensi
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +1,32 @@
|
||||
<?php
|
||||
|
||||
function is_palindrome(string $string, bool $caseInsensitive = true)
|
||||
/**
|
||||
* Checks if a string is a palindrome
|
||||
*
|
||||
* @param string $string
|
||||
* @param bool $caseInsensitive
|
||||
* @return bool
|
||||
*/
|
||||
function isPalindrome(string $string, bool $caseInsensitive = true)
|
||||
{
|
||||
$string = trim($string); // Removing leading and trailing spaces
|
||||
|
||||
if (empty($string)) {
|
||||
if (empty($string))
|
||||
{
|
||||
return false; // Returning false for an Empty String
|
||||
}
|
||||
|
||||
if ($caseInsensitive) {
|
||||
if ($caseInsensitive)
|
||||
{
|
||||
$string = strtolower($string); // Converting string to lowercase for case-insensitive check
|
||||
}
|
||||
|
||||
$characters = str_split($string);
|
||||
|
||||
for ($i = 0; $i < count($characters); $i++) {
|
||||
if ($characters[$i] !== $characters[count($characters) - ($i + 1)]) {
|
||||
for ($i = 0; $i < count($characters); $i++)
|
||||
{
|
||||
if ($characters[$i] !== $characters[count($characters) - ($i + 1)])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,12 +1,10 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This is a simple way to check palindrome string
|
||||
* using php strrev() function
|
||||
* This is a simple way to check palindrome string
|
||||
* using php strrev() function
|
||||
* make it simple
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $string
|
||||
* @param bool $caseInsensitive
|
||||
*/
|
||||
@ -15,19 +13,22 @@ function checkPalindromeString(string $string, bool $caseInsensitive = true): St
|
||||
//removing spaces
|
||||
$string = trim($string);
|
||||
|
||||
if (empty($string)) {
|
||||
if (empty($string))
|
||||
{
|
||||
throw new \Exception('You are given empty string. Please give a non-empty string value');
|
||||
}
|
||||
|
||||
/**
|
||||
* for case-insensitive
|
||||
* for case-insensitive
|
||||
* lowercase string conversion
|
||||
*/
|
||||
if ($caseInsensitive) {
|
||||
if ($caseInsensitive)
|
||||
{
|
||||
$string = strtolower($string);
|
||||
}
|
||||
|
||||
if ($string !== strrev($string)) {
|
||||
if ($string !== strrev($string))
|
||||
{
|
||||
return $string . " - not a palindrome string." . PHP_EOL;
|
||||
}
|
||||
|
31
Strings/CountConsonants.php
Normal file
31
Strings/CountConsonants.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Function returns the total number of consonants present in the given
|
||||
* string using a linear search through the string
|
||||
*
|
||||
* @param string $string
|
||||
* @return int
|
||||
*/
|
||||
function countConsonants(string $string): Int
|
||||
{
|
||||
if (empty($string)) {
|
||||
throw new \Exception('Please pass a non-empty string value');
|
||||
}
|
||||
|
||||
$vowels = ['a', 'e', 'i', 'o', 'u']; // Vowels Set
|
||||
$string = strtolower($string); // For case-insensitive checking
|
||||
|
||||
$consonantCount = 0;
|
||||
|
||||
for ($i = 0; $i < strlen($string); $i++)
|
||||
{
|
||||
if (!in_array($string[$i], $vowels) &&
|
||||
$string[$i] >= 'a' && $string[$i] <= 'z')
|
||||
{
|
||||
$consonantCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return $consonantCount;
|
||||
}
|
15
Strings/CountSentences.php
Normal file
15
Strings/CountSentences.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This is a simple way to count sentence
|
||||
* using php preg_match_all() function
|
||||
*
|
||||
* @param string $sentence
|
||||
* @return int
|
||||
*/
|
||||
function countSentences(string $sentence): int
|
||||
{
|
||||
$sentence = trim($sentence);
|
||||
|
||||
return preg_match_all('/[^\s|^\...](\.|\!|\?)(?!\w)/', $sentence);
|
||||
}
|
@ -1,50 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* This function returns
|
||||
* the Total no. of vowels
|
||||
* present in the given
|
||||
* string using a simple
|
||||
* method of looping
|
||||
* through all the
|
||||
* characters present in
|
||||
* the string.
|
||||
* This function returns the total number of vowels present in
|
||||
* the given string using a simple method of looping through
|
||||
* all the characters present in the string.
|
||||
*
|
||||
* @param string $string
|
||||
* @return int $noOfVowels
|
||||
* @return int $numberOfVowels
|
||||
*/
|
||||
function countVowelsSimple(string $string)
|
||||
{
|
||||
if (empty($string)) {
|
||||
if (empty($string))
|
||||
{
|
||||
throw new \Exception('Please pass a non-empty string value');
|
||||
}
|
||||
|
||||
$noOfVowels = 0;
|
||||
$numberOfVowels = 0;
|
||||
$vowels = ['a', 'e', 'i', 'o', 'u']; // Vowels Set
|
||||
$string = strtolower($string); // For case-insensitive checking
|
||||
$characters = str_split($string); // Splitting the string to a Character Array.
|
||||
|
||||
foreach ($characters as $character) {
|
||||
if (in_array($character, $vowels)) {
|
||||
$noOfVowels++;
|
||||
foreach ($characters as $character)
|
||||
{
|
||||
if (in_array($character, $vowels))
|
||||
{
|
||||
$numberOfVowels++;
|
||||
}
|
||||
}
|
||||
|
||||
return $noOfVowels;
|
||||
return $numberOfVowels;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns
|
||||
* the Total no. of vowels
|
||||
* present in the given
|
||||
* string using a
|
||||
* regular expression.
|
||||
* This function returns the Total number of vowels present in the given
|
||||
* string using a regular expression.
|
||||
*
|
||||
* @param string $string
|
||||
* @return int
|
||||
*/
|
||||
function countVowelsRegex(string $string)
|
||||
{
|
||||
if (empty($string)) {
|
||||
if (empty($string))
|
||||
{
|
||||
throw new \Exception('Please pass a non-empty string value');
|
||||
}
|
||||
$string = strtolower($string); // For case-insensitive checking
|
@ -1,13 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* Edit distance (Levenshtein distance).
|
||||
* Find distance (Levenshtein distance)
|
||||
*
|
||||
* Compute the difference between two sequences, ie, the minimum number of changes
|
||||
* to get to $str2 from $str1
|
||||
*
|
||||
* @param string $str1
|
||||
* @param string $str2
|
||||
* @return int the minimum number of changes to transform one string into another
|
||||
*/
|
||||
function editDistance($str1, $str2)
|
||||
function findDistance($str1, $str2)
|
||||
{
|
||||
$lenStr1 = strlen($str1);
|
||||
$lenStr2 = strlen($str2);
|
||||
@ -22,6 +24,7 @@ function editDistance($str1, $str2)
|
||||
|
||||
$distanceVectorInit = [];
|
||||
$distanceVectorFinal = [];
|
||||
|
||||
for ($i = 0; $i < $lenStr1 + 1; $i++) {
|
||||
$distanceVectorInit[] = 0;
|
||||
$distanceVectorFinal[] = 0;
|
||||
@ -33,9 +36,11 @@ function editDistance($str1, $str2)
|
||||
|
||||
for ($i = 0; $i < $lenStr2; $i++) {
|
||||
$distanceVectorFinal[0] = $i + 1;
|
||||
|
||||
// use formula to fill in the rest of the row
|
||||
for ($j = 0; $j < $lenStr1; $j++) {
|
||||
$substitutionCost = 0;
|
||||
|
||||
if ($str1[$j] == $str2[$i]) {
|
||||
$substitutionCost = $distanceVectorInit[$j];
|
||||
} else {
|
||||
@ -50,4 +55,4 @@ function editDistance($str1, $str2)
|
||||
|
||||
|
||||
return $distanceVectorFinal[$lenStr1];
|
||||
}
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* This function returns
|
||||
* the character which is
|
||||
* repeated maximum no. of
|
||||
* This function returns the character which is repeated maximum number of
|
||||
* times in the given string.
|
||||
*
|
||||
* @param string $string
|
||||
@ -20,9 +18,11 @@ function maxCharacter(string $string)
|
||||
|
||||
foreach ($characters as $character) {
|
||||
$currentCharacterCount = 1;
|
||||
|
||||
if (isset($characterCountTable[$character])) {
|
||||
$currentCharacterCount = $characterCountTable[$character] + 1;
|
||||
}
|
||||
|
||||
$characterCountTable[$character] = $currentCharacterCount;
|
||||
}
|
||||
|
@ -1,6 +1,12 @@
|
||||
<?php
|
||||
|
||||
function reverse_string(string $string)
|
||||
/**
|
||||
* This function returns a given string in reverse order
|
||||
*
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
function reverseString(string $string)
|
||||
{
|
||||
$string = trim($string); // Removing leading and trailing spaces
|
||||
|
22
Strings/ReverseWords.php
Normal file
22
Strings/ReverseWords.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This function returns a given sentence with its words
|
||||
* in reverse order
|
||||
*
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
function reverseWords(string $text)
|
||||
{
|
||||
$text = trim($text);
|
||||
$words = explode(' ', $text);
|
||||
$reversedWords = [];
|
||||
|
||||
for ($i = (count($words) - 1); $i >= 0; $i--)
|
||||
{
|
||||
$reversedWords[] = $words[$i];
|
||||
}
|
||||
|
||||
return implode(' ', $reversedWords);
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
<?php
|
||||
|
||||
use function PHPUnit\Framework\assertEquals;
|
||||
use function PHPUnit\Framework\assertFalse;
|
||||
use function PHPUnit\Framework\assertNotEquals;
|
||||
use function PHPUnit\Framework\assertTrue;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
require_once __DIR__ . '/../String/CheckPalindrome.php';
|
||||
require_once __DIR__ . '/../String/ReverseString.php';
|
||||
require_once __DIR__ . '/../String/ReverseWords.php';
|
||||
require_once __DIR__ . '/../String/CheckAnagram.php';
|
||||
require_once __DIR__ . '/../String/MaxCharacter.php';
|
||||
require_once __DIR__ . '/../String/CountVowels.php';
|
||||
require_once __DIR__ . '/../String/EditDistance.php';
|
||||
|
||||
class StringTest extends TestCase
|
||||
{
|
||||
public function testIsPalindrome()
|
||||
{
|
||||
assertTrue(is_palindrome('MaDam')); // true
|
||||
assertFalse(is_palindrome('ThisIsTest')); // false
|
||||
assertFalse(is_palindrome('')); // false
|
||||
assertTrue(is_palindrome('Sator Arepo Tenet Opera Rotas')); // true
|
||||
assertFalse(is_palindrome('Sator Arepo Tenet Opera Rotas', false)); // false since we are doing case-sensitive check
|
||||
}
|
||||
|
||||
public function testReverseString()
|
||||
{
|
||||
assertEquals('txet emos si sihT', reverse_string('This is some text'));
|
||||
assertEquals('mADaM', reverse_string('MaDAm'));
|
||||
assertNotEquals('MaDAm', reverse_string('MaDAm'));
|
||||
}
|
||||
|
||||
public function testReverseWords()
|
||||
{
|
||||
assertEquals('Fun is Coding PHP', reverse_words('PHP Coding is Fun'));
|
||||
assertEquals('OneWord', reverse_words('OneWord'));
|
||||
assertEquals('Text different some is This', reverse_words('This is some different Text'));
|
||||
}
|
||||
|
||||
public function testIsAnagram()
|
||||
{
|
||||
assertTrue(isAnagram("php", "PHP")); // By default it's case-insensitive
|
||||
assertFalse(isAnagram("php", "PHP", false)); // Make case-sensitive check
|
||||
assertTrue(isAnagram("php is fun", "pin hpf us"));
|
||||
assertFalse(isAnagram("Hello", " Hello")); //Extra space character
|
||||
assertTrue(isAnagram("ScRamble", "SRlmcaeb", false)); // Check with a mixture of upper and lower case
|
||||
}
|
||||
|
||||
public function testMaxCharacter()
|
||||
{
|
||||
assertEquals(maxCharacter("this is test for max character repetition"), 't');
|
||||
assertEquals(maxCharacter("This is Test for max characTer repetition"), 't');
|
||||
assertEquals(maxCharacter(" "), ' ');
|
||||
}
|
||||
|
||||
public function testCountVowels()
|
||||
{
|
||||
assertEquals(countVowelsSimple("This is a string with 7 vowels"), 7);
|
||||
assertEquals(countVowelsSimple("hello world"), 3);
|
||||
assertEquals(countVowelsRegex("Just A list of somE aaaaaaaaaa"), 16);
|
||||
|
||||
assertEquals(countVowelsRegex("This is a string with 7 vowels"), 7);
|
||||
assertEquals(countVowelsRegex("hello world"), 3);
|
||||
assertEquals(countVowelsRegex("Just A list of somE aaaaaaaaaa"), 16);
|
||||
}
|
||||
|
||||
public function testEditDistance()
|
||||
{
|
||||
assertEquals(editDistance("hello", "hallo"), 1);
|
||||
assertEquals(editDistance("hallo", "hello"), 1);
|
||||
assertEquals(editDistance("sunday", "sunday"), 0);
|
||||
assertEquals(editDistance("saturday", "sunday"), 3);
|
||||
}
|
||||
}
|
102
tests/Strings/StringsTest.php
Normal file
102
tests/Strings/StringsTest.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
use function PHPUnit\Framework\assertEquals;
|
||||
use function PHPUnit\Framework\assertFalse;
|
||||
use function PHPUnit\Framework\assertNotEquals;
|
||||
use function PHPUnit\Framework\assertTrue;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
require_once __DIR__ . '/../String/CheckPalindrome.php';
|
||||
require_once __DIR__ . '/../String/ReverseString.php';
|
||||
require_once __DIR__ . '/../String/ReverseWords.php';
|
||||
require_once __DIR__ . '/../String/CheckAnagram.php';
|
||||
require_once __DIR__ . '/../String/MaxCharacter.php';
|
||||
require_once __DIR__ . '/../String/CountVowels.php';
|
||||
require_once __DIR__ . '/../String/CountConsonants.php';
|
||||
require_once __DIR__ . '/../String/Distance.php';
|
||||
|
||||
class StringTest extends TestCase
|
||||
{
|
||||
public function testIsPalindrome()
|
||||
{
|
||||
assertTrue(isPalindrome('MaDam')); // true
|
||||
assertFalse(isPalindrome('ThisIsTest')); // false
|
||||
assertFalse(isPalindrome('')); // false
|
||||
assertTrue(isPalindrome('Sator Arepo Tenet Opera Rotas')); // true
|
||||
assertFalse(isPalindrome('Sator Arepo Tenet Opera Rotas', false)); // false since we are doing case-sensitive check
|
||||
}
|
||||
|
||||
public function testIsPalindrome2()
|
||||
{
|
||||
assertTrue(checkPalindromeString('131')); // true
|
||||
assertFalse(checkPalindromeString('roman')); // false
|
||||
assertTrue(checkPalindromeString('Level', true)); // true
|
||||
assertFalse(checkPalindromeString('Level', false)); // false
|
||||
assertFalse(checkPalindromeString('palindrome')); // false
|
||||
}
|
||||
|
||||
public function testCountSentences()
|
||||
{
|
||||
assertEquals(countSentences('Hi! Hello world.'), 2);
|
||||
assertEquals(countSentences('i am testing. test....'), 2);
|
||||
assertEquals(countSentences('How are you?'), 1);
|
||||
}
|
||||
|
||||
public function testReverseString()
|
||||
{
|
||||
assertEquals('txet emos si sihT', reverseString('This is some text'));
|
||||
assertEquals('mADaM', reverseString('MaDAm'));
|
||||
assertNotEquals('MaDAm', reverseString('MaDAm'));
|
||||
}
|
||||
|
||||
public function testReverseWords()
|
||||
{
|
||||
assertEquals('Fun is Coding PHP', reverseWords('PHP Coding is Fun'));
|
||||
assertEquals('OneWord', reverseWords('OneWord'));
|
||||
assertEquals('Text different some is This', reverseWords('This is some different Text'));
|
||||
}
|
||||
|
||||
public function testIsAnagram()
|
||||
{
|
||||
assertTrue(isAnagram("php", "PHP")); // By default it's case-insensitive
|
||||
assertFalse(isAnagram("php", "PHP", false)); // Make case-sensitive check
|
||||
assertTrue(isAnagram("php is fun", "pin hpf us"));
|
||||
assertFalse(isAnagram("Hello", " Hello")); //Extra space character
|
||||
assertTrue(isAnagram("ScRamble", "SRlmcaeb", false)); // Check with a mixture of upper and lower case
|
||||
}
|
||||
|
||||
public function testMaxCharacter()
|
||||
{
|
||||
assertEquals(maxCharacter("this is test for max character repetition"), 't');
|
||||
assertEquals(maxCharacter("This is Test for max characTer repetition"), 't');
|
||||
assertEquals(maxCharacter(" "), ' ');
|
||||
}
|
||||
|
||||
public function testCountVowels()
|
||||
{
|
||||
assertEquals(countVowelsSimple("This is a string with 7 vowels"), 7);
|
||||
assertEquals(countVowelsSimple("hello world"), 3);
|
||||
assertEquals(countVowelsRegex("Just A list of somE aaaaaaaaaa"), 16);
|
||||
|
||||
assertEquals(countVowelsRegex("This is a string with 7 vowels"), 7);
|
||||
assertEquals(countVowelsRegex("hello world"), 3);
|
||||
assertEquals(countVowelsRegex("Just A list of somE aaaaaaaaaa"), 16);
|
||||
}
|
||||
|
||||
public function testCountConsonants()
|
||||
{
|
||||
assertEquals(countConsonants("This is a string with 19 consonants"), 19);
|
||||
assertEquals(countConsonants("hello world"), 7);
|
||||
assertEquals(countConsonants("Just A list of somE aaaaaaaaaa"), 9);
|
||||
}
|
||||
|
||||
public function testFindDistance()
|
||||
{
|
||||
assertEquals(findDistance("hello", "hallo"), 1);
|
||||
assertEquals(findDistance("hallo", "hello"), 1);
|
||||
assertEquals(findDistance("sunday", "sunday"), 0);
|
||||
assertEquals(findDistance("saturday", "sunday"), 3);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user