Skip to content

Commit 658618a

Browse files
fix comment indents and docs
1 parent 29db480 commit 658618a

File tree

10 files changed

+98
-75
lines changed

10 files changed

+98
-75
lines changed

Ciphers/MonoAlphabeticCipher.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ function monoAlphabeticCipher($key, $alphabet, $text)
77
{
88
$cipherText = ''; // the cipher text (can be decrypted and encrypted)
99

10+
// check if the text length matches
1011
if (strlen($key) != strlen($alphabet)) {
1112
return false;
12-
} // check if the text length matches
13+
}
14+
1315
$text = preg_replace('/[0-9]+/', '', $text); // remove all the numbers
1416

1517
for ($i = 0; $i < strlen($text); $i++) {
@@ -20,6 +22,7 @@ function monoAlphabeticCipher($key, $alphabet, $text)
2022
$cipherText .= ( ctype_upper($text[$i]) ? strtoupper($key[$index]) : $key[$index] );
2123
}
2224
}
25+
2326
return $cipherText;
2427
}
2528

DataStructures/SinglyLinkedList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class SinglyLinkedList
77
{
88
public ?SinglyLinkedList $next = null;
99
public $data;
10+
1011
public function __construct($data)
1112
{
1213
$this->data = $data;

Maths/Factorial.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@
1212
*/
1313
function factorial(int $number)
1414
{
15-
static $cache = [];
16-
//internal caching memory for speed
15+
static $cache = []; //internal caching memory for speed
1716

1817
if ($number < 0) {
1918
throw new \Exception("Negative numbers are not allowed for calculating Factorial");
2019
}
20+
21+
// Factorial of 0 is 1
2122
if ($number === 0) {
2223
return 1;
23-
// Factorial of 0 is 1
2424
}
2525

2626
if (isset($cache[$number])) {
2727
return $cache[$number];
2828
}
2929

30+
// Recursion since x! = x * (x-1)!
3031
$fact = ($number * factorial($number - 1));
31-
// Recursion since x! = x * (x-1)!
32+
3233
$cache[$number] = $fact;
34+
3335
return $fact;
3436
}

Searches/ExponentialSearch.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ function binarySearch($arr, $value, $floor, $ceiling)
2020
{
2121
// Get $middle index
2222
$mid = floor(($floor + $ceiling) / 2);
23-
// Return position if $value is at the $mid position
23+
24+
// Return position if $value is at the $mid position
2425
if ($arr[$mid] === $value) {
2526
return (int) $mid;
2627
}
28+
2729
//Return -1 is range is wrong
2830
if ($floor > $ceiling) {
2931
return -1;
@@ -58,6 +60,7 @@ function exponentialSearch($arr, $value)
5860
}
5961
$floor = $i / 2;
6062
$ceiling = min($i, $length);
61-
// Call binary search for the range found above
63+
64+
// Call binary search for the range found above
6265
return binarySearch($arr, $value, $floor, $ceiling);
6366
}

Searches/InterpolationSearch.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,36 @@
1818
*/
1919
function interpolationSearch($arr, $key)
2020
{
21-
2221
$length = count($arr) - 1;
2322
$low = 0;
2423
$high = $length;
2524
$position = -1;
26-
//loop, between low & high
25+
//loop, between low & high
2726
while ($low <= $high && $key >= $arr[$low] && $key <= $arr[$high]) {
28-
//GET INDEX
27+
//GET INDEX
2928
$delta = ($key - $arr[$low]) / ($arr[$high] - $arr[$low]);
3029
$index = $low + floor(($high - $low) * $delta);
31-
//GET VALUE OF INDEX IN ARRAY...
30+
31+
//GET VALUE OF INDEX IN ARRAY...
3232
$indexValue = $arr[$index];
33+
3334
if ($indexValue === $key) {
34-
//index value equals key
35-
//FOUND TARGET
36-
//return index value
37-
$position = $index;
35+
//index value equals key
36+
//FOUND TARGET
37+
//return index value
38+
$position = $index;
3839
return (int) $position;
39-
}
40-
if ($indexValue < $key) {
41-
//index value lower than key
42-
//increase low index
40+
} elseif ($indexValue < $key) {
41+
//index value lower than key
42+
//increase low index
4343
$low = $index + 1;
44-
}
45-
if ($indexValue > $key) {
46-
//index value higher than key
47-
//decrease high index
44+
} elseif ($indexValue > $key) {
45+
//index value higher than key
46+
//decrease high index
4847
$high = $index - 1;
4948
}
5049
}
51-
//when key not found in array or array not sorted
50+
51+
//when key not found in array or array not sorted
5252
return null;
5353
}

Searches/JumpSearch.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ function jumpSearch($list, $key)
1414
{
1515
/*number of elements in the sorted array*/
1616
$num = count($list);
17-
/*block size to be jumped*/
17+
18+
/*block size to be jumped*/
1819
$step = (int)sqrt($num);
1920
$prev = 0;
2021

Searches/TernarySearch.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,75 +10,72 @@
1010
*/
1111
function ternarySearchByRecursion($arr, $key, $low, $high)
1212
{
13-
14-
//Return -1 if high lesser than low, we can't find item in the whole array
13+
// Return null if high is less than low (base case: key not found).
1514
if ($high < $low) {
1615
return null;
1716
}
1817

19-
//get $mid1 and $mid2
18+
// Calculate the indices of the first and second midpoints.
2019
$mid1 = floor($low + ($high - $low) / 3);
2120
$mid2 = floor($high - ($high - $low) / 3);
22-
// check if $key is found at any $mid
21+
22+
// Check if key is located at either midpoint.
2323
if ($arr[$mid1] === $key) {
24-
// return index of $key if found
2524
return $mid1;
2625
}
26+
2727
if ($arr[$mid2] === $key) {
28-
// return index of $key if found
2928
return $mid2;
3029
}
3130

32-
// since the $key is not found at $mid,
33-
// check in which region it is present
34-
// and repeat the Search operation
35-
// in that region
31+
// Determine which section to continue searching in.
3632
if ($key < $arr[$mid1]) {
37-
// the $key lies in between $low and $mid1
33+
// Key is in the left section, between $low and $mid1.
3834
return ternarySearchByRecursion($arr, $key, $low, $mid1 - 1);
3935
} elseif ($key > $arr[$mid2]) {
40-
// the $key lies in between $mid2 and $high
36+
// Key is in the right section, between $mid2 and $high.
4137
return ternarySearchByRecursion($arr, $key, $mid2 + 1, $high);
4238
} else {
43-
// the $key lies in between $mid1 and $mid2
39+
// Key is in the middle section, between $mid1 and $mid2.
4440
return ternarySearchByRecursion($arr, $key, $mid1 + 1, $mid2 - 1);
4541
}
4642
}
4743

4844
function ternarySearchIterative($arr, $key)
4945
{
46+
// Initialize low and high pointers.
5047
$low = 0;
5148
$high = count($arr) - 1;
49+
50+
// Continue searching while the high pointer is greater than or equal to the low pointer.
5251
while ($high >= $low) {
53-
// find the $mid1 and $mid2
52+
// Calculate the first and second midpoints.
5453
$mid1 = floor($low + ($high - $low) / 3);
5554
$mid2 = floor($high - ($high - $low) / 3);
56-
// check if $key is found at any $mid
55+
56+
// Check if the key is found at either midpoint.
5757
if ($arr[$mid1] === $key) {
58-
// return index of $key if found
5958
return $mid1;
6059
}
60+
6161
if ($arr[$mid2] === $key) {
62-
// return index of $key if found
6362
return $mid2;
6463
}
6564

66-
// since the $key is not found at $mid,
67-
// check in which region it is present
68-
// and repeat the Search operation
69-
// in that region
65+
// Determine the section to continue the search in.
7066
if ($key < $arr[$mid1]) {
71-
// the $key lies in between $low and $mid1
67+
// Key is in the left section, update the high pointer.
7268
$high = $mid1 - 1;
7369
} elseif ($key > $arr[$mid2]) {
74-
// the $key lies in between $mid2 and $high
70+
// Key is in the right section, update the low pointer.
7571
$low = $mid2 + 1;
7672
} else {
77-
// the $key lies in between $mid1 and $mid2
73+
// Key is in the middle section, update both pointers.
7874
$low = $mid1 + 1;
7975
$high = $mid2 - 1;
8076
}
8177
}
82-
// the $key was not found
78+
79+
// Key was not found.
8380
return null;
8481
}

Sorting/HeapSort.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,29 @@
1313
*/
1414
function heapSort(array $arr): array
1515
{
16+
// Get the number of elements in the array.
1617
$n = count($arr);
18+
19+
// Throw an exception if the array has no elements.
1720
if ($n <= 0) {
1821
throw new \UnexpectedValueException('Input array must have at least one element.');
1922
}
2023

21-
// Build heap
22-
for ($i = $n / 2 - 1; $i >= 0; $i--) {
24+
// Build a max heap from the array.
25+
for ($i = floor($n / 2) - 1; $i >= 0; $i--) {
2326
heapify($arr, $n, $i);
2427
}
2528

26-
// Extract elements from heap one by one
29+
// Extract elements from the max heap and build the sorted array.
2730
for ($i = $n - 1; $i >= 0; $i--) {
28-
// Swap
31+
// Swap the root(maximum value) of the heap with the last element of the heap.
2932
[$arr[0], $arr[$i]] = [$arr[$i], $arr[0]];
30-
// Heapify the reduced heap
33+
34+
// Heapify the reduced heap.
3135
heapify($arr, $i, 0);
3236
}
3337

38+
// Return the sorted array.
3439
return $arr;
3540
}
3641

Strings/CountVowels.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,31 @@
99
* @return int $numberOfVowels
1010
* @throws \Exception
1111
*/
12-
function countVowelsSimple(string $string)
12+
function countVowelsSimple(string $string): int
1313
{
14+
// Check for an empty string and throw an exception if so.
1415
if (empty($string)) {
15-
throw new \Exception('Please pass a non-empty string value');
16+
throw new \Exception('Please pass a non-empty string value.');
1617
}
1718

19+
// Initialize variables.
1820
$numberOfVowels = 0;
19-
$vowels = ['a', 'e', 'i', 'o', 'u'];
20-
// Vowels Set
21-
$string = strtolower($string);
22-
// For case-insensitive checking
21+
$vowels = ['a', 'e', 'i', 'o', 'u']; // Set of vowels for comparison.
22+
23+
// Convert the string to lowercase for case-insensitive comparison.
24+
$string = strtolower($string);
25+
26+
// Split the string into an array of characters.
2327
$characters = str_split($string);
24-
// Splitting the string to a Character Array.
2528

29+
// Loop through each character to count the vowels.
2630
foreach ($characters as $character) {
2731
if (in_array($character, $vowels)) {
2832
$numberOfVowels++;
2933
}
3034
}
3135

36+
// Return the total number of vowels found.
3237
return $numberOfVowels;
3338
}
3439

@@ -45,8 +50,8 @@ function countVowelsRegex(string $string)
4550
if (empty($string)) {
4651
throw new \Exception('Please pass a non-empty string value');
4752
}
48-
$string = strtolower($string);
49-
// For case-insensitive checking
53+
54+
$string = strtolower($string); // For case-insensitive checking
5055

5156
return preg_match_all('/[a,e,i,o,u]/', $string);
5257
}

Strings/MaxCharacter.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,34 @@
88
* @return string
99
* @throws \Exception
1010
*/
11-
function maxCharacter(string $string)
11+
function maxCharacter(string $string): string
1212
{
13+
// Throw an exception if the string is empty.
1314
if (empty($string)) {
14-
throw new \Exception('Please pass a non-empty string value');
15+
throw new \Exception('Please pass a non-empty string value.');
1516
}
1617

18+
// Initialize an associative array to hold character counts.
1719
$characterCountTable = [];
18-
// A variable to maintain the character counts
19-
$string = strtolower($string);
20-
// For case-insensitive checking
21-
$characters = str_split($string);
22-
// Splitting the string to a Character Array.
2320

21+
// Convert the string to lowercase for case-insensitive analysis.
22+
$string = strtolower($string);
23+
24+
// Convert the string into an array of characters.
25+
$characters = str_split($string);
26+
27+
// Loop through the characters to populate the count table.
2428
foreach ($characters as $character) {
25-
$currentCharacterCount = 1;
26-
if (isset($characterCountTable[$character])) {
27-
$currentCharacterCount = $characterCountTable[$character] + 1;
28-
}
29+
// Initialize or update the count of the current character.
30+
$currentCharacterCount = isset($characterCountTable[$character]) ? $characterCountTable[$character] + 1 : 1;
2931

32+
// Update the count in the table.
3033
$characterCountTable[$character] = $currentCharacterCount;
3134
}
3235

36+
// Sort the count table in descending order.
3337
arsort($characterCountTable);
38+
39+
// Return the character that appears most frequently.
3440
return array_keys($characterCountTable)[0];
3541
}

0 commit comments

Comments
 (0)