diff --git a/Ciphers/CaesarCipher.php b/Ciphers/CaesarCipher.php index 3af47ffe..18182f3d 100755 --- a/Ciphers/CaesarCipher.php +++ b/Ciphers/CaesarCipher.php @@ -3,8 +3,8 @@ /** * Encrypt given text using caesar cipher. * - * @param string text text to be encrypted - * @param int shift number of shifts to be applied + * @param string $text text text to be encrypted + * @param int $shift shift number of shifts to be applied * @return string new encrypted text */ function encrypt(string $text, int $shift): string @@ -27,8 +27,8 @@ function encrypt(string $text, int $shift): string /** * Decrypt given text using caesar cipher. - * @param string text text to be decrypted - * @param int shift number of shifts to be applied + * @param string $text text text to be decrypted + * @param int $shift shift number of shifts to be applied * @return string new decrypted text */ function decrypt(string $text, int $shift): string diff --git a/Ciphers/MonoAlphabeticCipher.php b/Ciphers/MonoAlphabeticCipher.php index 0810a4a5..6b362dbe 100644 --- a/Ciphers/MonoAlphabeticCipher.php +++ b/Ciphers/MonoAlphabeticCipher.php @@ -1,32 +1,34 @@ - \ No newline at end of file + 1 knot which is equal to 1 nautical mile (1852 km/h) * The conversion is made using kilometers as base * - * @param float $speed - * @param string $unitFrom - * @param string $unitTo - * @return int + * @param float $speed + * @param string $unitFrom + * @param string $unitTo + * @return float + * @throws \Exception */ function convertSpeed(float $speed, string $unitFrom, string $unitTo) { $speedUnitsFrom = [ 'mph' => 1.609344, 'km/h' => 1, - 'm/s'=> 3.6, - 'ft/s'=> 1.097, + 'm/s' => 3.6, + 'ft/s' => 1.097, 'kn' => 1.852, ]; $speedUnitsTo = [ 'mph' => 0.6213712, 'km/h' => 1, - 'm/s'=> 0.277778, - 'ft/s'=> 0.911344, + 'm/s' => 0.277778, + 'ft/s' => 0.911344, 'kn' => 0.539957, ]; $availableUnits = array_keys($speedUnitsFrom); diff --git a/DataStructures/SinglyLinkedList.php b/DataStructures/SinglyLinkedList.php index 44621d60..7256ed7d 100644 --- a/DataStructures/SinglyLinkedList.php +++ b/DataStructures/SinglyLinkedList.php @@ -1,13 +1,12 @@ data = $data; @@ -16,7 +15,6 @@ public function __construct($data) public function append($data): void { $current = $this; - while ($current instanceof SinglyLinkedList && isset($current->next)) { $current = $current->next; } @@ -27,7 +25,6 @@ public function append($data): void public function delete($data): SinglyLinkedList { $current = $this; - if ($current->data == $data) { return $current->next; } @@ -35,7 +32,6 @@ public function delete($data): SinglyLinkedList while ($current instanceof SinglyLinkedList && isset($current->next)) { if ($current->next->data === $data) { $current->next = $current->next->next; - return $this; } diff --git a/Graphs/BreadthFirstSearch.php b/Graphs/BreadthFirstSearch.php index 44110114..68579e10 100644 --- a/Graphs/BreadthFirstSearch.php +++ b/Graphs/BreadthFirstSearch.php @@ -3,21 +3,22 @@ /** * Breadth-first search (BFS) is an algorithm for searching a tree data structure for a node that satisfies a given property. * (https://en.wikipedia.org/wiki/Breadth-first_search). - * - * This is a non recursive implementation. - * + * + * This is a non-recursive implementation. + * * References: * https://cp-algorithms.com/graph/breadth-first-search.html - * + * * https://the-algorithms.com/algorithm/depth-first-search?lang=python - * + * * @author Aryansh Bhargavan https://github.com/aryanshb * @param array $adjList An array representing the grapth as an Adjacent List * @param int|string $start The starting vertex * @return bool if path between start and end vertex exists */ -function bfs($adjList, $start, $end, $yes = false){ +function bfs($adjList, $start, $end, $yes = false) +{ $visited = []; $queue = [$start]; while (!empty($queue)) { diff --git a/Graphs/DepthFirstSearch.php b/Graphs/DepthFirstSearch.php index 936484ce..3a8dea51 100644 --- a/Graphs/DepthFirstSearch.php +++ b/Graphs/DepthFirstSearch.php @@ -1,20 +1,20 @@ diff --git a/Maths/CheckPalindrome.php b/Maths/CheckPalindrome.php index 574e3407..d76b6da8 100644 --- a/Maths/CheckPalindrome.php +++ b/Maths/CheckPalindrome.php @@ -1,4 +1,5 @@ diff --git a/Maths/CheckPrime.php b/Maths/CheckPrime.php index 64630435..f9d14522 100644 --- a/Maths/CheckPrime.php +++ b/Maths/CheckPrime.php @@ -1,4 +1,5 @@ 0 && $set->valid()) - { + while ($i-- > 0 && $set->valid()) { yield $set->current(); $set->next(); } @@ -25,8 +24,7 @@ function fib() yield $i = 0; yield $j = 1; - while(true) - { + while (true) { yield $k = $i + $j; $i = $j; $j = $k; @@ -36,7 +34,6 @@ function fib() /* * Generate 100 Fibonacci numbers */ -foreach(loop(100, fib()) as $item) -{ - print($item.','); -} \ No newline at end of file +foreach (loop(100, fib()) as $item) { + print($item . ','); +} diff --git a/Maths/Mean.php b/Maths/Mean.php index 52f01202..b24b4e01 100644 --- a/Maths/Mean.php +++ b/Maths/Mean.php @@ -1,11 +1,13 @@ diff --git a/Maths/PerfectSquare.php b/Maths/PerfectSquare.php index 7bca0445..2b6fb747 100644 --- a/Maths/PerfectSquare.php +++ b/Maths/PerfectSquare.php @@ -1,4 +1,5 @@ $largest) { + if (strrev((string)$product) == (string)$product && $product > $largest) { $largest = $product; } } diff --git a/Maths/ProjectEuler/Problem6.php b/Maths/ProjectEuler/Problem6.php index 15a91ecb..35fd8e5c 100644 --- a/Maths/ProjectEuler/Problem6.php +++ b/Maths/ProjectEuler/Problem6.php @@ -5,10 +5,10 @@ * * Problem description: * The sum of the squares of the first ten natural numbers is, - * 1 ** 2 + 2 ** 2 + ... + 10 ** 2 = 385 + * 1 ** 2 + 2 ** 2 + ... + 10 ** 2 = 385 * * The square of the sum of the first ten natural numbers is, - * (1 + 2 + ... + 10) ** 2 = 3025 + * (1 + 2 + ... + 10) ** 2 = 3025 * * Hence the difference between the sum of the squares of the * first ten natural numbers and the square of the sum is . diff --git a/Maths/ProjectEuler/Problem8.php b/Maths/ProjectEuler/Problem8.php index 737254f1..aa0a215f 100644 --- a/Maths/ProjectEuler/Problem8.php +++ b/Maths/ProjectEuler/Problem8.php @@ -59,11 +59,11 @@ function problem8(): int $substringSize = 13; for ($i = 0; $i < strlen($theNumber) - $substringSize; $i++) { - $currentSubstring = substr($theNumber,$i,$substringSize); + $currentSubstring = substr($theNumber, $i, $substringSize); $currentProduct = 0; - for ($j = 0; $j < strlen ($currentSubstring); $j++) { - $currentProduct = ($currentProduct == 0 ? (int)$currentSubstring[$j] : $currentProduct * (int)$currentSubstring[$j]); + for ($j = 0; $j < strlen($currentSubstring); $j++) { + $currentProduct = ($currentProduct == 0 ? (int)$currentSubstring[$j] : $currentProduct * (int)$currentSubstring[$j]); } $greatestProduct = ($greatestProduct < $currentProduct ? $currentProduct : $greatestProduct); diff --git a/Searches/BinarySearch.php b/Searches/BinarySearch.php index e3b1e310..10789307 100644 --- a/Searches/BinarySearch.php +++ b/Searches/BinarySearch.php @@ -21,7 +21,7 @@ function binarySearchIterative($list, $target) { $first = 0; - $last = count($list)-1; + $last = count($list) - 1; while ($first <= $last) { @@ -70,8 +70,9 @@ function binarySearchByRecursion($list, $target, $start, $end) return $list[0] == $target ? 0 : null; } - if ($start > $end) + if ($start > $end) { return null; + } $mid = ($start + $end) >> 1; @@ -80,9 +81,9 @@ function binarySearchByRecursion($list, $target, $start, $end) if ($list[$mid] == $target) { return $mid; } elseif ($list[$mid] > $target) { - return binarySearchByRecursion($list, $target, $start, $mid-1); + return binarySearchByRecursion($list, $target, $start, $mid - 1); } elseif ($list[$mid] < $target) { - return binarySearchByRecursion($list, $target, $mid+1, $end); + return binarySearchByRecursion($list, $target, $mid + 1, $end); } return null; diff --git a/Searches/ExponentialSearch.php b/Searches/ExponentialSearch.php index 7073053d..f034c1f8 100644 --- a/Searches/ExponentialSearch.php +++ b/Searches/ExponentialSearch.php @@ -1,9 +1,10 @@ $ceiling) return -1; + if ($floor > $ceiling) { + return -1; + } // search the left part of the $array - // If the $middle element is great than the $value - if ($arr[$mid] > $value) { - return binarySearch($arr, $value, $floor, $mid - 1); - } + // If the $middle element is greater than the $value + if ($arr[$mid] > $value) { + return binarySearch($arr, $value, $floor, $mid - 1); + } // search the right part of the $array // If the $middle element is lower than the $value - else { - return binarySearch($arr, $value, $mid + 1, $ceiling); - } + else { + return binarySearch($arr, $value, $mid + 1, $ceiling); + } } - /** - * @param Array $arr - * @param int $length - * @param int $value - * @return int - **/ -function exponentialSearch ($arr, $value) { + +/** + * @param Array $arr + * @param int $value + * @return int + */ +function exponentialSearch($arr, $value) +{ + // If $value is the first element of the $array return this position - if ($arr[0] === $value) { - return 0; - } + if ($arr[0] === $value) { + return 0; + } // Find range for binary search - $i = 1; - $length = count($arr); - while ($i < $length && $arr[$i] <= $value) { - $i = $i * 2; - } - $floor = $i/2; - $ceiling = min($i, $length); - - // Call binary search for the range found above - return binarySearch($arr, $value, $floor, $ceiling); -} \ No newline at end of file + $i = 1; + $length = count($arr); + while ($i < $length && $arr[$i] <= $value) { + $i = $i * 2; + } + $floor = $i / 2; + $ceiling = min($i, $length); +// Call binary search for the range found above + return binarySearch($arr, $value, $floor, $ceiling); +} diff --git a/Searches/FibonacciSearch.php b/Searches/FibonacciSearch.php index 7e04a4b9..b103785a 100644 --- a/Searches/FibonacciSearch.php +++ b/Searches/FibonacciSearch.php @@ -11,13 +11,16 @@ */ function fibonacciPosition(int $n, array &$m = []) { - if(isset($m[$n])) return $m[$n]; - if($n < 2) return $n; + if (isset($m[$n])) { + return $m[$n]; + } + if ($n < 2) { + return $n; + } $m[$n] = fibonacciPosition($n - 1, $m) + fibonacciPosition($n - 2, $m); return $m[$n]; - } print fibonacciPosition(59); diff --git a/Searches/InterpolationSearch.php b/Searches/InterpolationSearch.php index 4e27c7dc..bb73d92d 100644 --- a/Searches/InterpolationSearch.php +++ b/Searches/InterpolationSearch.php @@ -1,12 +1,13 @@ key decrease the high index * repeat the loop */ -function interpolationSearch($arr, $key) { - $length = count($arr) - 1; - $low = 0; - $high = $length; - $position = -1; - //loop, between low & high - while ($low <= $high && $key >= $arr[$low] && $key <= $arr[$high]) { - //GET INDEX - $delta = ($key - $arr[$low]) / ($arr[$high] - $arr[$low]); - $index = $low + floor(($high - $low) * $delta); - //GET VALUE OF INDEX IN ARRAY... - $indexValue = $arr[$index]; - if ($indexValue === $key) { - //index value equals key - //FOUND TARGET - //return index value - $position = $index; - return (int) $position; +function interpolationSearch($arr, $key) +{ + + $length = count($arr) - 1; + $low = 0; + $high = $length; + $position = -1; +//loop, between low & high + while ($low <= $high && $key >= $arr[$low] && $key <= $arr[$high]) { +//GET INDEX + $delta = ($key - $arr[$low]) / ($arr[$high] - $arr[$low]); + $index = $low + floor(($high - $low) * $delta); +//GET VALUE OF INDEX IN ARRAY... + $indexValue = $arr[$index]; + if ($indexValue === $key) { + //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 + $low = $index + 1; + } + if ($indexValue > $key) { + //index value higher than key + //decrease high index + $high = $index - 1; + } } - if ($indexValue < $key) { - //index value lower than key - //increase low index - $low = $index + 1; - } - if ($indexValue > $key) { - //index value higher than key - //decrease high index - $high = $index - 1; - } - } //when key not found in array or array not sorted - return null; -} \ No newline at end of file + return null; +} diff --git a/Searches/JumpSearch.php b/Searches/JumpSearch.php index adda458b..7be70de6 100644 --- a/Searches/JumpSearch.php +++ b/Searches/JumpSearch.php @@ -1,40 +1,38 @@ = $num) - return -1; - } - /*Performing linear search for $key in block*/ - while ($list[$prev] < $key) - { - $prev++; - if ($prev == min($step, $num)) - return -1; - } - - return $list[$prev] === $key ? $prev : -1; -} +function jumpSearch($list, $key) +{ + /*number of elements in the sorted array*/ + $num = count($list); +/*block size to be jumped*/ + $step = (int)sqrt($num); + $prev = 0; + while ($list[min($step, $num) - 1] < $key) { + $prev = $step; + $step += (int)sqrt($num); + if ($prev >= $num) { + return -1; + } + } + /*Performing linear search for $key in block*/ + while ($list[$prev] < $key) { + $prev++; + if ($prev == min($step, $num)) { + return -1; + } + } + return $list[$prev] === $key ? $prev : -1; +} diff --git a/Searches/LinearSearch.php b/Searches/LinearSearch.php index 4de38be3..2d8ee41e 100644 --- a/Searches/LinearSearch.php +++ b/Searches/LinearSearch.php @@ -5,10 +5,6 @@ * * Reference: https://www.geeksforgeeks.org/linear-search/ * - * @param Array $list a array of integers to search - * @param integer $target an integer number to search for in the list - * @return integer the index where the target is found (or -1 if not found) - * * Examples: * data = 5, 7, 8, 11, 12, 15, 17, 18, 20 * x = 15 @@ -17,16 +13,15 @@ * x = 1 * Element not found * - * @param Array $list a array of integers to search + * @param Array $list an array of integers to search * @param integer $target an integer number to search for in the list * @return integer the index where the target is found (or -1 if not found) */ function linearSearch($list, $target) { $n = sizeof($list); - for($i = 0; $i < $n; $i++) - { - if($list[$i] == $target) { + for ($i = 0; $i < $n; $i++) { + if ($list[$i] == $target) { return $i + 1; } } diff --git a/Searches/LowerBound.php b/Searches/LowerBound.php index cf465555..ba4ddbb5 100644 --- a/Searches/LowerBound.php +++ b/Searches/LowerBound.php @@ -10,24 +10,25 @@ * [C++ Lower Bound](http://www.cplusplus.com/reference/algorithm/lower_bound/) * * It is assumed that an integer array is provided - * and the second parameter is also a integer + * and the second parameter is also an integer * - * @param array of sorted integers - * @param integer whose lower bound is to be found + * @param array $arr of sorted integers + * @param integer $elem whose lower bound is to be found * - * @return the index of lower bound of the given element + * @return int the index of lower bound of the given element */ -function lowerBound(array $arr, int $elem){ +function lowerBound(array $arr, int $elem) +{ isSortedAscendingInts($arr); $hi = count($arr); $lo = 0; - while($lo < $hi){ - $mid = $lo + floor(($hi - $lo)/2); + while ($lo < $hi) { + $mid = $lo + floor(($hi - $lo) / 2); - if($arr[$mid] < $elem){ - $lo = $mid+1; - }else{ + if ($arr[$mid] < $elem) { + $lo = $mid + 1; + } else { $hi = $mid; } } diff --git a/Searches/TernarySearch.php b/Searches/TernarySearch.php index a0e22353..21e4aad0 100644 --- a/Searches/TernarySearch.php +++ b/Searches/TernarySearch.php @@ -1,4 +1,5 @@ $arr[$mid2]) { - // the $key lies in between $mid2 and $high - return ternarySearchByRecursion($arr, $key, $mid2 + 1, $high); +// the $key lies in between $low and $mid1 + return ternarySearchByRecursion($arr, $key, $low, $mid1 - 1); + } elseif ($key > $arr[$mid2]) { + // the $key lies in between $mid2 and $high + return ternarySearchByRecursion($arr, $key, $mid2 + 1, $high); } else { - // the $key lies in between $mid1 and $mid2 - return ternarySearchByRecursion($arr, $key, $mid1 + 1, $mid2 - 1); + // the $key lies in between $mid1 and $mid2 + return ternarySearchByRecursion($arr, $key, $mid1 + 1, $mid2 - 1); } } -function ternarySearchIterative ($arr, $key) { - $low = 0; $high = count($arr) - 1; - while ($high >= $low) { +function ternarySearchIterative($arr, $key) +{ + $low = 0; + $high = count($arr) - 1; + while ($high >= $low) { // find the $mid1 and $mid2 - $mid1 = floor($low + ($high - $low) / 3); - $mid2 = floor($high - ($high - $low) / 3); - + $mid1 = floor($low + ($high - $low) / 3); + $mid2 = floor($high - ($high - $low) / 3); // check if $key is found at any $mid - if ($arr[$mid1] === $key) { - // return index of $key if found - return $mid1; - } - if ($arr[$mid2] === $key) { - // return index of $key if found - return $mid2; - } + 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 - if ($key < $arr[$mid1]) { - // the $key lies in between $low and $mid1 - $high = $mid1 - 1; - } else if ($key > $arr[$mid2]) { - // the $key lies in between $mid2 and $high - $low = $mid2 + 1; - } else { - // the $key lies in between $mid1 and $mid2 - $low = $mid1 + 1; - $high = $mid2 - 1; + // since the $key is not found at $mid, + // check in which region it is present + // and repeat the Search operation + // in that region + if ($key < $arr[$mid1]) { + // the $key lies in between $low and $mid1 + $high = $mid1 - 1; + } elseif ($key > $arr[$mid2]) { + // the $key lies in between $mid2 and $high + $low = $mid2 + 1; + } else { + // the $key lies in between $mid1 and $mid2 + $low = $mid1 + 1; + $high = $mid2 - 1; + } } - } // the $key was not found - return null; + return null; } diff --git a/Searches/UpperBound.php b/Searches/UpperBound.php index 6cfbffe3..47d62518 100644 --- a/Searches/UpperBound.php +++ b/Searches/UpperBound.php @@ -10,24 +10,25 @@ * [C++ Lower Bound](http://www.cplusplus.com/reference/algorithm/upper_bound/) * * It is assumed that an integer array is provided - * and the second parameter is also a integer + * and the second parameter is also an integer * - * @param array of sorted integers - * @param integer whose upper bound is to be found + * @param array $arr of sorted integers + * @param integer $elem whose upper bound is to be found * - * @return the index of upper bound of the given element + * @return int the index of upper bound of the given element */ -function upperBound(array $arr, int $elem){ +function upperBound(array $arr, int $elem) +{ isSortedAscendingInts($arr); $hi = count($arr); $lo = 0; - while($lo < $hi){ - $mid = $lo + floor(($hi - $lo)/2); + while ($lo < $hi) { + $mid = $lo + floor(($hi - $lo) / 2); - if($arr[$mid] <= $elem){ + if ($arr[$mid] <= $elem) { $lo = $mid + 1; - }else{ + } else { $hi = $mid; } } diff --git a/Sorting/ArrayKeysSort.php b/Sorting/ArrayKeysSort.php index 034258ec..4c97830d 100644 --- a/Sorting/ArrayKeysSort.php +++ b/Sorting/ArrayKeysSort.php @@ -1,4 +1,5 @@ $key) || !isset($b->$key)) { - $errorMsg = 'The key "' . $key - . '" does not exist in the collection'; - throw new Exception($errorMsg); - } - $item1 = !$isCaseSensitive - ? strtolower($a->$key) : $a->$key; - $item2 = !$isCaseSensitive - ? strtolower($b->$key) : $b->$key; + throw new Exception($errorMsg); } - } while ($item1 === $item2 && !empty($keys[++$pos])); - - if ($item1 === $item2) { - return 0; - } elseif ($order === self::ORDER_ASC) { - return ($item1 < $item2) ? -1 : 1; + $item1 = !$isCaseSensitive + ? strtolower($a[$key]) : $a[$key]; + $item2 = !$isCaseSensitive + ? strtolower($b[$key]) : $b[$key]; } else { - return ($item1 > $item2) ? -1 : 1; + if (!isset($a->$key) || !isset($b->$key)) { + $errorMsg = 'The key "' . $key + . '" does not exist in the collection'; + throw new Exception($errorMsg); + } + $item1 = !$isCaseSensitive + ? strtolower($a->$key) : $a->$key; + $item2 = !$isCaseSensitive + ? strtolower($b->$key) : $b->$key; } + } while ($item1 === $item2 && !empty($keys[++$pos])); + if ($item1 === $item2) { + return 0; + } elseif ($order === self::ORDER_ASC) { + return ($item1 < $item2) ? -1 : 1; + } else { + return ($item1 > $item2) ? -1 : 1; } - ); + }); } catch (Exception $e) { echo $e->getMessage(); die(); diff --git a/Sorting/BubbleSort.php b/Sorting/BubbleSort.php index b46e7a0b..9bc66795 100644 --- a/Sorting/BubbleSort.php +++ b/Sorting/BubbleSort.php @@ -6,13 +6,14 @@ * @param array $array * @return array */ -function bubbleSort($array) { +function bubbleSort($array) +{ $length = count($array); for ($i = $length; $i > 0; $i--) { $swapped = true; - for ($j=0;$j<$i-1;$j++) { + for ($j = 0; $j < $i - 1; $j++) { if ($array[$j] > $array[$j + 1]) { $temp = $array[$j]; $array[$j] = $array[$j + 1]; @@ -21,7 +22,9 @@ function bubbleSort($array) { } } - if ($swapped) break; + if ($swapped) { + break; + } } return $array; diff --git a/Sorting/BubbleSort2.php b/Sorting/BubbleSort2.php index f29b3394..97d0757f 100644 --- a/Sorting/BubbleSort2.php +++ b/Sorting/BubbleSort2.php @@ -1,4 +1,5 @@ 0 ) { + for ($i = $min; $i <= $max; $i++) { + while ($count[$i]-- > 0) { $array[$z++] = $i; } } diff --git a/Sorting/GnomeSort.php b/Sorting/GnomeSort.php index 003e0970..a9a88ce8 100644 --- a/Sorting/GnomeSort.php +++ b/Sorting/GnomeSort.php @@ -4,26 +4,25 @@ * Gnome Sort * References: * https://www.geeksforgeeks.org/gnome-sort-a-stupid-one/ - * + * * The Gnome algorithm works by locating the first instance in which two adjoining elements are arranged incorrectly and swaps with each other. - * + * * @param array $array refers to the array to be sorted * @return array */ - -function gnomeSort($array){ +function gnomeSort($array) +{ $a = 1; $b = 2; - while($a < count($array)){ - - if ($array[$a-1] <= $array[$a]){ + while ($a < count($array)) { + if ($array[$a - 1] <= $array[$a]) { $a = $b; $b++; - }else{ - list($array[$a],$array[$a-1]) = array($array[$a-1],$array[$a]); + } else { + list($array[$a],$array[$a - 1]) = array($array[$a - 1],$array[$a]); $a--; - if($a == 0){ + if ($a == 0) { $a = $b; $b++; } diff --git a/Sorting/HeapSort.php b/Sorting/HeapSort.php index 308b2201..d999967b 100644 --- a/Sorting/HeapSort.php +++ b/Sorting/HeapSort.php @@ -1,4 +1,5 @@ = 0; $i--) { - // Swap +// Swap [$arr[0], $arr[$i]] = [$arr[$i], $arr[0]]; - - // Heapify the reduced heap +// Heapify the reduced heap heapify($arr, $i, 0); } @@ -46,7 +46,6 @@ function heapify(array &$arr, int $n, int $i): void $largest = $i; $left = 2 * $i + 1; $right = 2 * $i + 2; - if ($left < $n && $arr[$left] > $arr[$largest]) { $largest = $left; } @@ -59,4 +58,4 @@ function heapify(array &$arr, int $n, int $i): void [$arr[$i], $arr[$largest]] = [$arr[$largest], $arr[$i]]; heapify($arr, $n, $largest); } -} \ No newline at end of file +} diff --git a/Sorting/InsertionSort.php b/Sorting/InsertionSort.php index b184e545..fcffaa00 100644 --- a/Sorting/InsertionSort.php +++ b/Sorting/InsertionSort.php @@ -8,13 +8,11 @@ */ function insertionSort(array $array) { - for ($i = 1; $i < count($array); $i++) - { + for ($i = 1; $i < count($array); $i++) { $currentVal = $array[$i]; - for ($j = $i - 1; $j >= 0 && $array[$j] > $currentVal; $j--) - { - $array[$j + 1] = $array[$j]; + for ($j = $i - 1; $j >= 0 && $array[$j] > $currentVal; $j--) { + $array[$j + 1] = $array[$j]; } $array[$j + 1] = $currentVal; @@ -22,5 +20,3 @@ function insertionSort(array $array) return $array; } - - diff --git a/Sorting/MergeSort.php b/Sorting/MergeSort.php index 7923917a..23c64178 100644 --- a/Sorting/MergeSort.php +++ b/Sorting/MergeSort.php @@ -9,12 +9,12 @@ function mergeSort(array $arr) { if (count($arr) <= 1) { - return $arr; + return $arr; } - $mid = floor( count($arr) / 2 ); - $leftArray = mergeSort( array_slice($arr, 0, $mid) ); - $rightArray = mergeSort( array_slice($arr, $mid) ); + $mid = floor(count($arr) / 2); + $leftArray = mergeSort(array_slice($arr, 0, $mid)); + $rightArray = mergeSort(array_slice($arr, $mid)); return merge($leftArray, $rightArray); } @@ -52,6 +52,3 @@ function merge(array $leftArray, array $rightArray) return $result; } - - - diff --git a/Sorting/QuickSort.php b/Sorting/QuickSort.php index da35617b..0950effa 100644 --- a/Sorting/QuickSort.php +++ b/Sorting/QuickSort.php @@ -1,4 +1,5 @@ = 'a' && $string[$i] <= 'z') - { + for ($i = 0; $i < strlen($string); $i++) { + if ( + !in_array($string[$i], $vowels) && + $string[$i] >= 'a' && $string[$i] <= 'z' + ) { $consonantCount++; } } diff --git a/Strings/CountVowels.php b/Strings/CountVowels.php index e3eef0a5..34a78c0f 100644 --- a/Strings/CountVowels.php +++ b/Strings/CountVowels.php @@ -1,28 +1,30 @@ = 0; $i--) - { + for ($i = (count($words) - 1); $i >= 0; $i--) { $reversedWords[] = $words[$i]; } diff --git a/Utils/ArrayHelpers.php b/Utils/ArrayHelpers.php index 9666aa61..37602e99 100644 --- a/Utils/ArrayHelpers.php +++ b/Utils/ArrayHelpers.php @@ -1,24 +1,25 @@ assertEquals( $input_str, xorCipher( xorCipher( $input_str , $key) , $key)); - $this->assertNotEquals( $input_str, xorCipher( xorCipher( $input_str , $key) , $invalid_key)); + $this->assertEquals($input_str, xorCipher(xorCipher($input_str, $key), $key)); + $this->assertNotEquals($input_str, xorCipher(xorCipher($input_str, $key), $invalid_key)); } } diff --git a/tests/Ciphers/MonoAlphabeticCipherTest.php b/tests/Ciphers/MonoAlphabeticCipherTest.php index 4509ab4c..de900967 100644 --- a/tests/Ciphers/MonoAlphabeticCipherTest.php +++ b/tests/Ciphers/MonoAlphabeticCipherTest.php @@ -1,23 +1,21 @@ - +assertTrue($ans); } } - diff --git a/tests/Maths/MathsTest.php b/tests/Maths/MathsTest.php index 8684a738..d3bc9b47 100644 --- a/tests/Maths/MathsTest.php +++ b/tests/Maths/MathsTest.php @@ -3,6 +3,7 @@ use function PHPUnit\Framework\assertEquals; use function PHPUnit\Framework\assertFalse; use function PHPUnit\Framework\assertTrue; + use PHPUnit\Framework\TestCase; require_once __DIR__ . '/../../vendor/autoload.php'; @@ -107,7 +108,7 @@ public function testNeonNumber() assertFalse(isNumberNeon(123)); assertTrue(isNumberNeon(9)); } - + public function testFibonacciGenerator() { assertEquals([0, 1, 1, 2, 3], iterator_to_array(loop(5, fib()))); @@ -120,15 +121,15 @@ public function testFibonacciGenerator() public function testMean() { assertEquals( - (2 + 4 + 6 + 8 + 20 + 50 + 70) / 7, + (2 + 4 + 6 + 8 + 20 + 50 + 70) / 7, mean(2, 4, 6, 8, 20, 50, 70) ); assertEquals( - (-5 - 7 + 10) / 3, + (-5 - 7 + 10) / 3, mean(-5, -7, 10) ); - + assertEquals(-1, mean(-1)); } @@ -146,5 +147,4 @@ public function testMode() $this->assertEquals([1, 2, 3, 4, 5], mode(1, 2, 3, 4, 5)); $this->assertEquals([2, 3, 4], mode(2, 2, 3, 3, 4, 4)); } - } diff --git a/tests/Maths/ProjectEulerTest.php b/tests/Maths/ProjectEulerTest.php index 3b74f3ed..43b1da47 100644 --- a/tests/Maths/ProjectEulerTest.php +++ b/tests/Maths/ProjectEulerTest.php @@ -62,7 +62,7 @@ public function testProblem9(): void { $this->assertSame(31875000, problem9()); } - + public function testProblem10(): void { $this->assertSame(142913828922, problem10()); diff --git a/tests/Searches/SearchesTest.php b/tests/Searches/SearchesTest.php index 08d2f716..3de89db9 100644 --- a/tests/Searches/SearchesTest.php +++ b/tests/Searches/SearchesTest.php @@ -3,6 +3,7 @@ use function PHPUnit\Framework\assertEquals; use function PHPUnit\Framework\assertFalse; use function PHPUnit\Framework\assertTrue; + use PHPUnit\Framework\TestCase; require_once __DIR__ . '/../../vendor/autoload.php'; @@ -138,7 +139,7 @@ public function testUpperBound() $result = upperBound($list, $target); assertEquals(5, $result); } - + public function testJumpSearch() { $list = array( 3,5,6,7,9,10,12,20,22,24); diff --git a/tests/Sorting/ArrayKeysSortTest.php b/tests/Sorting/ArrayKeysSortTest.php index 2b054987..31c322df 100644 --- a/tests/Sorting/ArrayKeysSortTest.php +++ b/tests/Sorting/ArrayKeysSortTest.php @@ -7,7 +7,8 @@ class ArrayKeysSortTest extends TestCase { - public function testArrayKeysSort() { + public function testArrayKeysSort() + { //test array of arrays $array1 = [ ['fruit' => 'banana', 'color' => 'yellow', 'cant' => 5], @@ -36,22 +37,22 @@ public function testArrayKeysSort() { $this->assertEquals($result2, $test2); //test array of objects - $object1 = new \stdClass; + $object1 = new \stdClass(); $object1->fruit = 'banana'; $object1->color = 'yellow'; $object1->cant = 5; - $object2 = new \stdClass; + $object2 = new \stdClass(); $object2->fruit = 'apple'; $object2->color = 'red'; $object2->cant = 2; - $object3 = new \stdClass; + $object3 = new \stdClass(); $object3->fruit = 'apple'; $object3->color = 'green'; $object3->cant = 7; - $object4 = new \stdClass; + $object4 = new \stdClass(); $object4->fruit = 'grapes'; $object4->color = 'purple'; $object4->cant = 4; diff --git a/tests/Strings/StringsTest.php b/tests/Strings/StringsTest.php index 3b17907e..41cd170e 100644 --- a/tests/Strings/StringsTest.php +++ b/tests/Strings/StringsTest.php @@ -4,6 +4,7 @@ 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';