mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-03-11 02:07:49 +01:00
fix comment indents and docs
This commit is contained in:
parent
29db48075e
commit
658618ae95
@ -7,9 +7,11 @@ function monoAlphabeticCipher($key, $alphabet, $text)
|
|||||||
{
|
{
|
||||||
$cipherText = ''; // the cipher text (can be decrypted and encrypted)
|
$cipherText = ''; // the cipher text (can be decrypted and encrypted)
|
||||||
|
|
||||||
|
// check if the text length matches
|
||||||
if (strlen($key) != strlen($alphabet)) {
|
if (strlen($key) != strlen($alphabet)) {
|
||||||
return false;
|
return false;
|
||||||
} // check if the text length matches
|
}
|
||||||
|
|
||||||
$text = preg_replace('/[0-9]+/', '', $text); // remove all the numbers
|
$text = preg_replace('/[0-9]+/', '', $text); // remove all the numbers
|
||||||
|
|
||||||
for ($i = 0; $i < strlen($text); $i++) {
|
for ($i = 0; $i < strlen($text); $i++) {
|
||||||
@ -20,6 +22,7 @@ function monoAlphabeticCipher($key, $alphabet, $text)
|
|||||||
$cipherText .= ( ctype_upper($text[$i]) ? strtoupper($key[$index]) : $key[$index] );
|
$cipherText .= ( ctype_upper($text[$i]) ? strtoupper($key[$index]) : $key[$index] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $cipherText;
|
return $cipherText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ class SinglyLinkedList
|
|||||||
{
|
{
|
||||||
public ?SinglyLinkedList $next = null;
|
public ?SinglyLinkedList $next = null;
|
||||||
public $data;
|
public $data;
|
||||||
|
|
||||||
public function __construct($data)
|
public function __construct($data)
|
||||||
{
|
{
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
|
@ -12,23 +12,25 @@
|
|||||||
*/
|
*/
|
||||||
function factorial(int $number)
|
function factorial(int $number)
|
||||||
{
|
{
|
||||||
static $cache = [];
|
static $cache = []; //internal caching memory for speed
|
||||||
//internal caching memory for speed
|
|
||||||
|
|
||||||
if ($number < 0) {
|
if ($number < 0) {
|
||||||
throw new \Exception("Negative numbers are not allowed for calculating Factorial");
|
throw new \Exception("Negative numbers are not allowed for calculating Factorial");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Factorial of 0 is 1
|
||||||
if ($number === 0) {
|
if ($number === 0) {
|
||||||
return 1;
|
return 1;
|
||||||
// Factorial of 0 is 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($cache[$number])) {
|
if (isset($cache[$number])) {
|
||||||
return $cache[$number];
|
return $cache[$number];
|
||||||
}
|
}
|
||||||
|
|
||||||
$fact = ($number * factorial($number - 1));
|
|
||||||
// Recursion since x! = x * (x-1)!
|
// Recursion since x! = x * (x-1)!
|
||||||
|
$fact = ($number * factorial($number - 1));
|
||||||
|
|
||||||
$cache[$number] = $fact;
|
$cache[$number] = $fact;
|
||||||
|
|
||||||
return $fact;
|
return $fact;
|
||||||
}
|
}
|
||||||
|
@ -20,10 +20,12 @@ function binarySearch($arr, $value, $floor, $ceiling)
|
|||||||
{
|
{
|
||||||
// Get $middle index
|
// Get $middle index
|
||||||
$mid = floor(($floor + $ceiling) / 2);
|
$mid = floor(($floor + $ceiling) / 2);
|
||||||
|
|
||||||
// Return position if $value is at the $mid position
|
// Return position if $value is at the $mid position
|
||||||
if ($arr[$mid] === $value) {
|
if ($arr[$mid] === $value) {
|
||||||
return (int) $mid;
|
return (int) $mid;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Return -1 is range is wrong
|
//Return -1 is range is wrong
|
||||||
if ($floor > $ceiling) {
|
if ($floor > $ceiling) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -58,6 +60,7 @@ function exponentialSearch($arr, $value)
|
|||||||
}
|
}
|
||||||
$floor = $i / 2;
|
$floor = $i / 2;
|
||||||
$ceiling = min($i, $length);
|
$ceiling = min($i, $length);
|
||||||
|
|
||||||
// Call binary search for the range found above
|
// Call binary search for the range found above
|
||||||
return binarySearch($arr, $value, $floor, $ceiling);
|
return binarySearch($arr, $value, $floor, $ceiling);
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
*/
|
*/
|
||||||
function interpolationSearch($arr, $key)
|
function interpolationSearch($arr, $key)
|
||||||
{
|
{
|
||||||
|
|
||||||
$length = count($arr) - 1;
|
$length = count($arr) - 1;
|
||||||
$low = 0;
|
$low = 0;
|
||||||
$high = $length;
|
$high = $length;
|
||||||
@ -28,26 +27,27 @@ function interpolationSearch($arr, $key)
|
|||||||
//GET INDEX
|
//GET INDEX
|
||||||
$delta = ($key - $arr[$low]) / ($arr[$high] - $arr[$low]);
|
$delta = ($key - $arr[$low]) / ($arr[$high] - $arr[$low]);
|
||||||
$index = $low + floor(($high - $low) * $delta);
|
$index = $low + floor(($high - $low) * $delta);
|
||||||
|
|
||||||
//GET VALUE OF INDEX IN ARRAY...
|
//GET VALUE OF INDEX IN ARRAY...
|
||||||
$indexValue = $arr[$index];
|
$indexValue = $arr[$index];
|
||||||
|
|
||||||
if ($indexValue === $key) {
|
if ($indexValue === $key) {
|
||||||
//index value equals key
|
//index value equals key
|
||||||
//FOUND TARGET
|
//FOUND TARGET
|
||||||
//return index value
|
//return index value
|
||||||
$position = $index;
|
$position = $index;
|
||||||
return (int) $position;
|
return (int) $position;
|
||||||
}
|
} elseif ($indexValue < $key) {
|
||||||
if ($indexValue < $key) {
|
|
||||||
//index value lower than key
|
//index value lower than key
|
||||||
//increase low index
|
//increase low index
|
||||||
$low = $index + 1;
|
$low = $index + 1;
|
||||||
}
|
} elseif ($indexValue > $key) {
|
||||||
if ($indexValue > $key) {
|
|
||||||
//index value higher than key
|
//index value higher than key
|
||||||
//decrease high index
|
//decrease high index
|
||||||
$high = $index - 1;
|
$high = $index - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//when key not found in array or array not sorted
|
//when key not found in array or array not sorted
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ function jumpSearch($list, $key)
|
|||||||
{
|
{
|
||||||
/*number of elements in the sorted array*/
|
/*number of elements in the sorted array*/
|
||||||
$num = count($list);
|
$num = count($list);
|
||||||
|
|
||||||
/*block size to be jumped*/
|
/*block size to be jumped*/
|
||||||
$step = (int)sqrt($num);
|
$step = (int)sqrt($num);
|
||||||
$prev = 0;
|
$prev = 0;
|
||||||
|
@ -10,75 +10,72 @@
|
|||||||
*/
|
*/
|
||||||
function ternarySearchByRecursion($arr, $key, $low, $high)
|
function ternarySearchByRecursion($arr, $key, $low, $high)
|
||||||
{
|
{
|
||||||
|
// Return null if high is less than low (base case: key not found).
|
||||||
//Return -1 if high lesser than low, we can't find item in the whole array
|
|
||||||
if ($high < $low) {
|
if ($high < $low) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//get $mid1 and $mid2
|
// Calculate the indices of the first and second midpoints.
|
||||||
$mid1 = floor($low + ($high - $low) / 3);
|
$mid1 = floor($low + ($high - $low) / 3);
|
||||||
$mid2 = floor($high - ($high - $low) / 3);
|
$mid2 = floor($high - ($high - $low) / 3);
|
||||||
// check if $key is found at any $mid
|
|
||||||
|
// Check if key is located at either midpoint.
|
||||||
if ($arr[$mid1] === $key) {
|
if ($arr[$mid1] === $key) {
|
||||||
// return index of $key if found
|
|
||||||
return $mid1;
|
return $mid1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($arr[$mid2] === $key) {
|
if ($arr[$mid2] === $key) {
|
||||||
// return index of $key if found
|
|
||||||
return $mid2;
|
return $mid2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// since the $key is not found at $mid,
|
// Determine which section to continue searching in.
|
||||||
// check in which region it is present
|
|
||||||
// and repeat the Search operation
|
|
||||||
// in that region
|
|
||||||
if ($key < $arr[$mid1]) {
|
if ($key < $arr[$mid1]) {
|
||||||
// the $key lies in between $low and $mid1
|
// Key is in the left section, between $low and $mid1.
|
||||||
return ternarySearchByRecursion($arr, $key, $low, $mid1 - 1);
|
return ternarySearchByRecursion($arr, $key, $low, $mid1 - 1);
|
||||||
} elseif ($key > $arr[$mid2]) {
|
} elseif ($key > $arr[$mid2]) {
|
||||||
// the $key lies in between $mid2 and $high
|
// Key is in the right section, between $mid2 and $high.
|
||||||
return ternarySearchByRecursion($arr, $key, $mid2 + 1, $high);
|
return ternarySearchByRecursion($arr, $key, $mid2 + 1, $high);
|
||||||
} else {
|
} else {
|
||||||
// the $key lies in between $mid1 and $mid2
|
// Key is in the middle section, between $mid1 and $mid2.
|
||||||
return ternarySearchByRecursion($arr, $key, $mid1 + 1, $mid2 - 1);
|
return ternarySearchByRecursion($arr, $key, $mid1 + 1, $mid2 - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function ternarySearchIterative($arr, $key)
|
function ternarySearchIterative($arr, $key)
|
||||||
{
|
{
|
||||||
|
// Initialize low and high pointers.
|
||||||
$low = 0;
|
$low = 0;
|
||||||
$high = count($arr) - 1;
|
$high = count($arr) - 1;
|
||||||
|
|
||||||
|
// Continue searching while the high pointer is greater than or equal to the low pointer.
|
||||||
while ($high >= $low) {
|
while ($high >= $low) {
|
||||||
// find the $mid1 and $mid2
|
// Calculate the first and second midpoints.
|
||||||
$mid1 = floor($low + ($high - $low) / 3);
|
$mid1 = floor($low + ($high - $low) / 3);
|
||||||
$mid2 = floor($high - ($high - $low) / 3);
|
$mid2 = floor($high - ($high - $low) / 3);
|
||||||
// check if $key is found at any $mid
|
|
||||||
|
// Check if the key is found at either midpoint.
|
||||||
if ($arr[$mid1] === $key) {
|
if ($arr[$mid1] === $key) {
|
||||||
// return index of $key if found
|
|
||||||
return $mid1;
|
return $mid1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($arr[$mid2] === $key) {
|
if ($arr[$mid2] === $key) {
|
||||||
// return index of $key if found
|
|
||||||
return $mid2;
|
return $mid2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// since the $key is not found at $mid,
|
// Determine the section to continue the search in.
|
||||||
// check in which region it is present
|
|
||||||
// and repeat the Search operation
|
|
||||||
// in that region
|
|
||||||
if ($key < $arr[$mid1]) {
|
if ($key < $arr[$mid1]) {
|
||||||
// the $key lies in between $low and $mid1
|
// Key is in the left section, update the high pointer.
|
||||||
$high = $mid1 - 1;
|
$high = $mid1 - 1;
|
||||||
} elseif ($key > $arr[$mid2]) {
|
} elseif ($key > $arr[$mid2]) {
|
||||||
// the $key lies in between $mid2 and $high
|
// Key is in the right section, update the low pointer.
|
||||||
$low = $mid2 + 1;
|
$low = $mid2 + 1;
|
||||||
} else {
|
} else {
|
||||||
// the $key lies in between $mid1 and $mid2
|
// Key is in the middle section, update both pointers.
|
||||||
$low = $mid1 + 1;
|
$low = $mid1 + 1;
|
||||||
$high = $mid2 - 1;
|
$high = $mid2 - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// the $key was not found
|
|
||||||
|
// Key was not found.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -13,24 +13,29 @@
|
|||||||
*/
|
*/
|
||||||
function heapSort(array $arr): array
|
function heapSort(array $arr): array
|
||||||
{
|
{
|
||||||
|
// Get the number of elements in the array.
|
||||||
$n = count($arr);
|
$n = count($arr);
|
||||||
|
|
||||||
|
// Throw an exception if the array has no elements.
|
||||||
if ($n <= 0) {
|
if ($n <= 0) {
|
||||||
throw new \UnexpectedValueException('Input array must have at least one element.');
|
throw new \UnexpectedValueException('Input array must have at least one element.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build heap
|
// Build a max heap from the array.
|
||||||
for ($i = $n / 2 - 1; $i >= 0; $i--) {
|
for ($i = floor($n / 2) - 1; $i >= 0; $i--) {
|
||||||
heapify($arr, $n, $i);
|
heapify($arr, $n, $i);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract elements from heap one by one
|
// Extract elements from the max heap and build the sorted array.
|
||||||
for ($i = $n - 1; $i >= 0; $i--) {
|
for ($i = $n - 1; $i >= 0; $i--) {
|
||||||
// Swap
|
// Swap the root(maximum value) of the heap with the last element of the heap.
|
||||||
[$arr[0], $arr[$i]] = [$arr[$i], $arr[0]];
|
[$arr[0], $arr[$i]] = [$arr[$i], $arr[0]];
|
||||||
// Heapify the reduced heap
|
|
||||||
|
// Heapify the reduced heap.
|
||||||
heapify($arr, $i, 0);
|
heapify($arr, $i, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return the sorted array.
|
||||||
return $arr;
|
return $arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,26 +9,31 @@
|
|||||||
* @return int $numberOfVowels
|
* @return int $numberOfVowels
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
function countVowelsSimple(string $string)
|
function countVowelsSimple(string $string): int
|
||||||
{
|
{
|
||||||
|
// Check for an empty string and throw an exception if so.
|
||||||
if (empty($string)) {
|
if (empty($string)) {
|
||||||
throw new \Exception('Please pass a non-empty string value');
|
throw new \Exception('Please pass a non-empty string value.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize variables.
|
||||||
$numberOfVowels = 0;
|
$numberOfVowels = 0;
|
||||||
$vowels = ['a', 'e', 'i', 'o', 'u'];
|
$vowels = ['a', 'e', 'i', 'o', 'u']; // Set of vowels for comparison.
|
||||||
// Vowels Set
|
|
||||||
$string = strtolower($string);
|
|
||||||
// For case-insensitive checking
|
|
||||||
$characters = str_split($string);
|
|
||||||
// Splitting the string to a Character Array.
|
|
||||||
|
|
||||||
|
// Convert the string to lowercase for case-insensitive comparison.
|
||||||
|
$string = strtolower($string);
|
||||||
|
|
||||||
|
// Split the string into an array of characters.
|
||||||
|
$characters = str_split($string);
|
||||||
|
|
||||||
|
// Loop through each character to count the vowels.
|
||||||
foreach ($characters as $character) {
|
foreach ($characters as $character) {
|
||||||
if (in_array($character, $vowels)) {
|
if (in_array($character, $vowels)) {
|
||||||
$numberOfVowels++;
|
$numberOfVowels++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return the total number of vowels found.
|
||||||
return $numberOfVowels;
|
return $numberOfVowels;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,8 +50,8 @@ function countVowelsRegex(string $string)
|
|||||||
if (empty($string)) {
|
if (empty($string)) {
|
||||||
throw new \Exception('Please pass a non-empty string value');
|
throw new \Exception('Please pass a non-empty string value');
|
||||||
}
|
}
|
||||||
$string = strtolower($string);
|
|
||||||
// For case-insensitive checking
|
$string = strtolower($string); // For case-insensitive checking
|
||||||
|
|
||||||
return preg_match_all('/[a,e,i,o,u]/', $string);
|
return preg_match_all('/[a,e,i,o,u]/', $string);
|
||||||
}
|
}
|
||||||
|
@ -8,28 +8,34 @@
|
|||||||
* @return string
|
* @return string
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
function maxCharacter(string $string)
|
function maxCharacter(string $string): string
|
||||||
{
|
{
|
||||||
|
// Throw an exception if the string is empty.
|
||||||
if (empty($string)) {
|
if (empty($string)) {
|
||||||
throw new \Exception('Please pass a non-empty string value');
|
throw new \Exception('Please pass a non-empty string value.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize an associative array to hold character counts.
|
||||||
$characterCountTable = [];
|
$characterCountTable = [];
|
||||||
// A variable to maintain the character counts
|
|
||||||
|
// Convert the string to lowercase for case-insensitive analysis.
|
||||||
$string = strtolower($string);
|
$string = strtolower($string);
|
||||||
// For case-insensitive checking
|
|
||||||
|
// Convert the string into an array of characters.
|
||||||
$characters = str_split($string);
|
$characters = str_split($string);
|
||||||
// Splitting the string to a Character Array.
|
|
||||||
|
|
||||||
|
// Loop through the characters to populate the count table.
|
||||||
foreach ($characters as $character) {
|
foreach ($characters as $character) {
|
||||||
$currentCharacterCount = 1;
|
// Initialize or update the count of the current character.
|
||||||
if (isset($characterCountTable[$character])) {
|
$currentCharacterCount = isset($characterCountTable[$character]) ? $characterCountTable[$character] + 1 : 1;
|
||||||
$currentCharacterCount = $characterCountTable[$character] + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Update the count in the table.
|
||||||
$characterCountTable[$character] = $currentCharacterCount;
|
$characterCountTable[$character] = $currentCharacterCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sort the count table in descending order.
|
||||||
arsort($characterCountTable);
|
arsort($characterCountTable);
|
||||||
|
|
||||||
|
// Return the character that appears most frequently.
|
||||||
return array_keys($characterCountTable)[0];
|
return array_keys($characterCountTable)[0];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user