Skip to content

Commit

Permalink
fix comment indents and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 committed Oct 5, 2023
1 parent 29db480 commit 6e797ce
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 50 deletions.
5 changes: 4 additions & 1 deletion Ciphers/MonoAlphabeticCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ function monoAlphabeticCipher($key, $alphabet, $text)
{
$cipherText = ''; // the cipher text (can be decrypted and encrypted)

// check if the text length matches
if (strlen($key) != strlen($alphabet)) {
return false;
} // check if the text length matches
}

$text = preg_replace('/[0-9]+/', '', $text); // remove all the numbers

for ($i = 0; $i < strlen($text); $i++) {
Expand All @@ -20,6 +22,7 @@ function monoAlphabeticCipher($key, $alphabet, $text)
$cipherText .= ( ctype_upper($text[$i]) ? strtoupper($key[$index]) : $key[$index] );
}
}

return $cipherText;
}

Expand Down
1 change: 1 addition & 0 deletions DataStructures/SinglyLinkedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SinglyLinkedList
{
public ?SinglyLinkedList $next = null;
public $data;

public function __construct($data)
{
$this->data = $data;
Expand Down
10 changes: 6 additions & 4 deletions Maths/Factorial.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@
*/
function factorial(int $number)
{
static $cache = [];
//internal caching memory for speed
static $cache = []; //internal caching memory for speed

if ($number < 0) {
throw new \Exception("Negative numbers are not allowed for calculating Factorial");
}

// Factorial of 0 is 1
if ($number === 0) {
return 1;
// Factorial of 0 is 1
}

if (isset($cache[$number])) {
return $cache[$number];
}

// Recursion since x! = x * (x-1)!
$fact = ($number * factorial($number - 1));
// Recursion since x! = x * (x-1)!

$cache[$number] = $fact;

return $fact;
}
7 changes: 5 additions & 2 deletions Searches/ExponentialSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ function binarySearch($arr, $value, $floor, $ceiling)
{
// Get $middle index
$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) {
return (int) $mid;
}

//Return -1 is range is wrong
if ($floor > $ceiling) {
return -1;
Expand Down Expand Up @@ -58,6 +60,7 @@ function exponentialSearch($arr, $value)
}
$floor = $i / 2;
$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);
}
34 changes: 17 additions & 17 deletions Searches/InterpolationSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@
*/
function interpolationSearch($arr, $key)
{

$length = count($arr) - 1;
$low = 0;
$high = $length;
$position = -1;
//loop, between low & high
//loop, between low & high
while ($low <= $high && $key >= $arr[$low] && $key <= $arr[$high]) {
//GET INDEX
//GET INDEX
$delta = ($key - $arr[$low]) / ($arr[$high] - $arr[$low]);
$index = $low + floor(($high - $low) * $delta);
//GET VALUE OF INDEX IN ARRAY...

//GET VALUE OF INDEX IN ARRAY...
$indexValue = $arr[$index];

if ($indexValue === $key) {
//index value equals key
//FOUND TARGET
//return index value
$position = $index;
//index value equals key
//FOUND TARGET
//return index value
$position = $index;
return (int) $position;
}
if ($indexValue < $key) {
//index value lower than key
//increase low index
} elseif ($indexValue < $key) {
//index value lower than key
//increase low index
$low = $index + 1;
}
if ($indexValue > $key) {
//index value higher than key
//decrease high index
} elseif ($indexValue > $key) {
//index value higher than key
//decrease high index
$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;
}
3 changes: 2 additions & 1 deletion Searches/JumpSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function jumpSearch($list, $key)
{
/*number of elements in the sorted array*/
$num = count($list);
/*block size to be jumped*/

/*block size to be jumped*/
$step = (int)sqrt($num);
$prev = 0;

Expand Down
47 changes: 22 additions & 25 deletions Searches/TernarySearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,72 @@
*/
function ternarySearchByRecursion($arr, $key, $low, $high)
{

//Return -1 if high lesser than low, we can't find item in the whole array
// Return null if high is less than low (base case: key not found).
if ($high < $low) {
return null;
}

//get $mid1 and $mid2
// Calculate the indices of the first and second midpoints.
$mid1 = floor($low + ($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) {
// return index of $key if found
return $mid1;
}

if ($arr[$mid2] === $key) {
// return index of $key if found
return $mid2;
}

// since the $key is not found at $mid,
// check in which region it is present
// and repeat the Search operation
// in that region
// Determine which section to continue searching in.
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);
} 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);
} 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);
}
}

function ternarySearchIterative($arr, $key)
{
// Initialize low and high pointers.
$low = 0;
$high = count($arr) - 1;

// Continue searching while the high pointer is greater than or equal to the low pointer.
while ($high >= $low) {
// find the $mid1 and $mid2
// Calculate the first and second midpoints.
$mid1 = floor($low + ($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) {
// return index of $key if found
return $mid1;
}

if ($arr[$mid2] === $key) {
// return index of $key if found
return $mid2;
}

// since the $key is not found at $mid,
// check in which region it is present
// and repeat the Search operation
// in that region
// Determine the section to continue the search in.
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;
} 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;
} else {
// the $key lies in between $mid1 and $mid2
// Key is in the middle section, update both pointers.
$low = $mid1 + 1;
$high = $mid2 - 1;
}
}
// the $key was not found

// Key was not found.
return null;
}

0 comments on commit 6e797ce

Please sign in to comment.