Skip to content

Commit

Permalink
fix cs errors to follow the psr12 standard and also some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 committed Oct 4, 2023
1 parent d7d34e3 commit ebd1fa8
Show file tree
Hide file tree
Showing 71 changed files with 571 additions and 574 deletions.
8 changes: 4 additions & 4 deletions Ciphers/CaesarCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
66 changes: 34 additions & 32 deletions Ciphers/MonoAlphabeticCipher.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
<?php
// A mono-alphabetic cipher is a simple substitution cipher
// https://www.101computing.net/mono-alphabetic-substitution-cipher/

function monoAlphabeticCipher($key, $alphabet, $text){

$cipherText = ''; // the cipher text (can be decrypted and encrypted)

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++ ){
$index = strripos( $alphabet, $text[$i] );
if( $text[$i] == " " ){ $cipherText .= " "; }
else{ $cipherText .= ( ctype_upper($text[$i]) ? strtoupper($key[$index]) : $key[$index] ); }
}
return $cipherText;
}

function maEncrypt($key, $alphabet, $text){

return monoAlphabeticCipher($key, $alphabet, $text);

}

function maDecrypt($key, $alphabet, $text){

return monoAlphabeticCipher($alphabet, $key, $text);

}

?>
<?php

// A mono-alphabetic cipher is a simple substitution cipher
// https://www.101computing.net/mono-alphabetic-substitution-cipher/

function monoAlphabeticCipher($key, $alphabet, $text)
{
$cipherText = ''; // the cipher text (can be decrypted and encrypted)

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++) {
$index = strripos($alphabet, $text[$i]);
if ($text[$i] == " ") {
$cipherText .= " ";
} else {
$cipherText .= ( ctype_upper($text[$i]) ? strtoupper($key[$index]) : $key[$index] );
}
}
return $cipherText;
}

function maEncrypt($key, $alphabet, $text)
{
return monoAlphabeticCipher($key, $alphabet, $text);
}

function maDecrypt($key, $alphabet, $text)
{
return monoAlphabeticCipher($alphabet, $key, $text);
}
8 changes: 5 additions & 3 deletions Ciphers/MorseCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
/**
* Encode text to Morse Code.
*
* @param string text to encode
* @param string $text text to encode
* @return string encoded text
* @throws \Exception
*/
function encode(string $text): string
{
Expand Down Expand Up @@ -57,13 +58,14 @@ function encode(string $text): string
throw new \Exception("Invalid character: $c");
}
}
substr_replace($encodedText ,"", -1); // Removes trailing space
substr_replace($encodedText, "", -1); // Removes trailing space
return $encodedText;
}

/**
* Decode Morse Code to text.
* @param string text to decode
* @param string $text text to decode
* @throws \Exception
*/
function decode(string $text): string
{
Expand Down
6 changes: 2 additions & 4 deletions Ciphers/XORCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
* character with the key.
* The key is repeated until it is the same length as the input.
*
* @param string $input The input string.
* @param string $input_string The input string.
* @param string $key The key to use.
* @return string The encrypted string.
*/
function xorCipher(string $input_string, string $key)
{

$key_len = strlen($key);
$result = array();

for ($idx = 0; $idx < strlen($input_string); $idx++)
{
for ($idx = 0; $idx < strlen($input_string); $idx++) {
array_push($result, $input_string[$idx] ^ $key[$idx % $key_len]);
}

Expand Down
3 changes: 2 additions & 1 deletion Conversions/BinaryToDecimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
* (2 + 0) base 10
* 2 base 10
*
* @param string $binaryNumber
* @param string $binaryNumber
* @return int
* @throws \Exception
*/
function binaryToDecimal($binaryNumber)
{
Expand Down
3 changes: 2 additions & 1 deletion Conversions/DecimalToBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* submitted Decimal Number to
* Binary Number.
*
* @param string $decimalNumber
* @param string $decimalNumber
* @return string
* @throws \Exception
*/
function decimalToBinary($decimalNumber)
{
Expand Down
6 changes: 4 additions & 2 deletions Conversions/OctalToDecimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
* (1 * (8 ^ 1) + 0 * (8 ^ 0)) base 10
* (8 + 0) base 10
* 9 base 10
* @param string $octalNumber
* @param string $octalNumber
* @return int
* @throws \Exception
*/
function octalToDecimal($octalNumber)
{
Expand All @@ -34,8 +35,9 @@ function octalToDecimal($octalNumber)
* submitted Decimal Number to
* Octal Number.
*
* @param string $decimalNumber
* @param string $decimalNumber
* @return string
* @throws \Exception
*/
function decimalToOctal($decimalNumber)
{
Expand Down
17 changes: 9 additions & 8 deletions Conversions/SpeedConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@
* kn -> 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);
Expand Down
6 changes: 1 addition & 5 deletions DataStructures/SinglyLinkedList.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

/**
* Singly Linked List
*/

class SinglyLinkedList
{
public ?SinglyLinkedList $next = null;
public $data;

public function __construct($data)
{
$this->data = $data;
Expand All @@ -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;
}
Expand All @@ -27,15 +25,13 @@ public function append($data): void
public function delete($data): SinglyLinkedList
{
$current = $this;

if ($current->data == $data) {
return $current->next;
}

while ($current instanceof SinglyLinkedList && isset($current->next)) {
if ($current->next->data === $data) {
$current->next = $current->next->next;

return $this;
}

Expand Down
13 changes: 7 additions & 6 deletions Graphs/BreadthFirstSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
17 changes: 9 additions & 8 deletions Graphs/DepthFirstSearch.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php

/**
* The Depth-first Search is an algorithm used for traversing or searching trees or graphs
* The Depth-first Search is an algorithm used for traversing or searching trees or graphs
* (https://en.wikipedia.org/wiki/Depth-first_search).
*
* This is a non recursive implementation.
*
*
* This is a non-recursive implementation.
*
* References:
* https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/
*
*
* https://the-algorithms.com/algorithm/depth-first-search?lang=python
*
*
* @author Andre Alves https://github.com/andremralves
* @param array $adjList An array representing the grapth as an Adjacent List
* @param int|string $start The starting vertex
* @return array The visited vertices
* @return array The visited vertices
*/
function dfs($adjList, $start)
{
Expand All @@ -27,8 +27,9 @@ function dfs($adjList, $start)

// Checks each adjacent vertex of $v and add to the stack if not visited
foreach (array_reverse($adjList[$v]) as $adj) {
if (!array_key_exists($adj, $visited))
if (!array_key_exists($adj, $visited)) {
array_push($stack, $adj);
}
}
}
return array_keys($visited);
Expand Down
4 changes: 3 additions & 1 deletion Maths/AbsoluteMax.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

/**
* This function calculates
* Absolute max values from
* the different numbers
* provided
*
* @param decimal $numbers A variable sized number input
* @param decimal $numbers A variable sized number input
* @return decimal $absoluteMax Absolute max value
* @throws \Exception
*/
function absolute_max(...$numbers)
{
Expand Down
4 changes: 3 additions & 1 deletion Maths/AbsoluteMin.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

/**
* This function calculates
* Absolute min values from
* the different numbers
* provided.
*
* @param decimal $numbers A variable sized number input
* @param decimal $numbers A variable sized number input
* @return decimal $absoluteMin Absolute min value
* @throws \Exception
*/
function absolute_min(...$numbers)
{
Expand Down
23 changes: 11 additions & 12 deletions Maths/ArmstrongNumber.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This function checks if given number is Armstrong
* e.g. 153
Expand All @@ -7,16 +8,14 @@
*/
function isNumberArmstrong(int $input): bool
{
$arr = array_map('intval', str_split($input));
$sumOfCubes = 0;
foreach ($arr as $num) {
$sumOfCubes += $num * $num * $num;
}
if ($sumOfCubes == $input) {
return true;
}
else {
return false;
}
$arr = array_map('intval', str_split($input));
$sumOfCubes = 0;
foreach ($arr as $num) {
$sumOfCubes += $num * $num * $num;
}
if ($sumOfCubes == $input) {
return true;
} else {
return false;
}
}
?>
Loading

0 comments on commit ebd1fa8

Please sign in to comment.