Skip to content

Commit

Permalink
Merge branch 'master' into SentinelSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
darwinz authored Jun 14, 2024
2 parents 71de3bc + 5d9350b commit f241d8d
Show file tree
Hide file tree
Showing 32 changed files with 1,380 additions and 24 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
ini-values: xdebug.max_nesting_level=512

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/directory_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ jobs:
name: DIRECTORY.md
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1 # v2 is broken for git diff
- uses: actions/setup-python@v2
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- name: Setup Git Specs
run: |
git config --global user.name github-actions
git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'
git config --global user.name "$GITHUB_ACTOR"
git config --global user.email "$[email protected]"
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
- name: Update DIRECTORY.md
shell: python
Expand Down
1 change: 1 addition & 0 deletions .gitpod.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM gitpod/workspace-full:2022-05-08-14-31-53
6 changes: 6 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
image:
file: .gitpod.Dockerfile

tasks:
- init: |
echo "Welcome to TheAlgorithms/PHP"
39 changes: 39 additions & 0 deletions Ciphers/AtbashCipher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* Encrypt a message using the Atbash Cipher.
* The Atbash Cipher is a simple substitution cipher where each letter in the plaintext is
* replaced with its corresponding letter from the end of the alphabet (reverse alphabet).
* Non-alphabet characters are not modified.
*
* @param string $plainText The plaintext to encrypt.
* @return string The encrypted message.
*/
function atbash_encrypt($plainText)
{
$result = '';
$plainText = strtoupper($plainText);
for ($i = 0; $i < strlen($plainText); $i++) {
$char = $plainText[$i];
if (ctype_alpha($char)) {
$offset = ord('Z') - ord($char);
$encryptedChar = chr(ord('A') + $offset);
} else {
$encryptedChar = $char; // Non-alphabet characters remain unchanged
}
$result .= $encryptedChar;
}
return $result;
}

/**
* Decrypt a message encrypted using the Atbash Cipher.
* Since the Atbash Cipher is its own inverse, decryption is the same as encryption.
*
* @param string $cipherText The ciphertext to decrypt.
* @return string The decrypted message.
*/
function atbash_decrypt($cipherText)
{
return atbash_encrypt($cipherText); // Decryption is the same as encryption
}
81 changes: 81 additions & 0 deletions Ciphers/RailfenceCipher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* Encode a message using the Rail Fence Cipher.
* (https://en.wikipedia.org/wiki/Rail_fence_cipher)
*
* @param string $plainMessage The message to encode.
* @param int $rails The number of rails or rows in the rail fence.
*
* @return string The encoded message.
*/
function Railencode($plainMessage, $rails): string
{
$cipherMessage = [];
$position = ($rails * 2) - 2;
// Iterate through the characters of the plain message
for ($index = 0; $index < strlen($plainMessage); $index++) {
for ($step = 0; $step < $rails; $step++) {
if (!isset($cipherMessage[$step])) {
$cipherMessage[$step] = '';
}
// Check if the character should go in the rail
if ($index % $position == $step || $index % $position == $position - $step) {
$cipherMessage[$step] .= $plainMessage[$index];
} else {
// Add a placeholder for empty spaces
$cipherMessage[$step] .= ".";
}
}
}
// Combine and remove placeholders to form the cipher message
return implode('', str_replace('.', '', $cipherMessage));
}

/**
* Decode a message encoded using the Rail Fence Cipher.
*
* @param string $cipherMessage The encoded message.
* @param int $rails The number of rails or rows used for encoding.
*
* @return string The decoded plain message.
*/
function Raildecode($cipherMessage, $rails): string
{
$position = ($rails * 2) - 2;
$textLength = strlen($cipherMessage);
$minLength = floor($textLength / $position);
$balance = $textLength % $position;
$lengths = [];
$strings = [];
$totalLengths = 0;
// Calculate the number of characters in each row
for ($rowIndex = 0; $rowIndex < $rails; $rowIndex++) {
$lengths[$rowIndex] = $minLength;
if ($rowIndex != 0 && $rowIndex != ($rails - 1)) {
$lengths[$rowIndex] += $minLength;
}
if ($balance > $rowIndex) {
$lengths[$rowIndex]++;
}
if ($balance > ($rails + ($rails - $rowIndex) - 2)) {
$lengths[$rowIndex]++;
}
$strings[] = substr($cipherMessage, $totalLengths, $lengths[$rowIndex]);
$totalLengths += $lengths[$rowIndex];
}
// Convert the rows of characters to plain message
$plainText = '';
while (strlen($plainText) < $textLength) {
for ($charIndex = 0; $charIndex < $position; $charIndex++) {
if (isset($strings[$charIndex])) {
$index = $charIndex;
} else {
$index = $position - $charIndex;
}
$plainText .= substr($strings[$index], 0, 1);
$strings[$index] = substr($strings[$index], 1);
}
}
return $plainText;
}
63 changes: 63 additions & 0 deletions Ciphers/VignereCipher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Encrypts a plaintext using the Vigenère cipher.
* (https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher)
*
* @param string $plaintext The plaintext to be encrypted.
* @param string $key The encryption key.
* @return string The encrypted text.
*/
function vigenere_encrypt($plaintext, $key): string
{
// Convert the input to uppercase for consistency
$plaintext = strtoupper($plaintext);
$key = strtoupper($key);
$keyLength = strlen($key);
$encryptedText = "";
for ($i = 0; $i < strlen($plaintext); $i++) {
$char = $plaintext[$i];
if (ctype_alpha($char)) {
// Calculate the shift based on the key
$shift = ord($key[$i % $keyLength]) - ord('A');
// Apply the Vigenère encryption formula
$encryptedChar = chr(((ord($char) - ord('A') + $shift) % 26) + ord('A'));
// Append the encrypted character to the result
$encryptedText .= $encryptedChar;
} else {
// If the character is not alphabetic, leave it unchanged
$encryptedText .= $char;
}
}
return $encryptedText;
}

/**
* Decrypts a ciphertext using the Vigenère cipher.
*
* @param string $ciphertext The ciphertext to be decrypted.
* @param string $key The decryption key.
* @return string The decrypted text.
*/
function vigenere_decrypt($ciphertext, $key): string
{
$ciphertext = strtoupper($ciphertext);
$key = strtoupper($key);
$keyLength = strlen($key);
$decryptedText = "";
for ($i = 0; $i < strlen($ciphertext); $i++) {
$char = $ciphertext[$i];
if (ctype_alpha($char)) {
// Calculate the shift based on the key
$shift = ord($key[$i % $keyLength]) - ord('A');
// Apply the Vigenère decryption formula
$decryptedChar = chr(((ord($char) - ord('A') - $shift + 26) % 26) + ord('A'));
// Append the decrypted character to the result
$decryptedText .= $decryptedChar;
} else {
// If the character is not alphabetic, leave it unchanged
$decryptedText .= $char;
}
}
return $decryptedText;
}
20 changes: 19 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# List of all files

## Ciphers
* [Atbashcipher](./Ciphers/AtbashCipher.php)
* [Caesarcipher](./Ciphers/CaesarCipher.php)
* [Monoalphabeticcipher](./Ciphers/MonoAlphabeticCipher.php)
* [Morsecode](./Ciphers/MorseCode.php)
* [Railfencecipher](./Ciphers/RailfenceCipher.php)
* [Vignerecipher](./Ciphers/VignereCipher.php)
* [Xorcipher](./Ciphers/XORCipher.php)

## Conversions
Expand All @@ -14,22 +17,29 @@
* [Speedconversion](./Conversions/SpeedConversion.php)

## Datastructures
* [Doublylinkedlist](./DataStructures/DoublyLinkedList.php)
* [Node](./DataStructures/Node.php)
* [Singlylinkedlist](./DataStructures/SinglyLinkedList.php)
* [Stack](./DataStructures/Stack.php)

## Graphs
* [Bellmanford](./Graphs/BellmanFord.php)
* [Breadthfirstsearch](./Graphs/BreadthFirstSearch.php)
* [Depthfirstsearch](./Graphs/DepthFirstSearch.php)

## Maths
* [Absolutemax](./Maths/AbsoluteMax.php)
* [Absolutemin](./Maths/AbsoluteMin.php)
* [Armstrongnumber](./Maths/ArmstrongNumber.php)
* [Basex](./Maths/BaseX.php)
* [Checkpalindrome](./Maths/CheckPalindrome.php)
* [Checkprime](./Maths/CheckPrime.php)
* [Factorial](./Maths/Factorial.php)
* [Fastexponentiation](./Maths/FastExponentiation.php)
* [Fastinversesquareroot](./Maths/FastInverseSquareRoot.php)
* [Fibonacci](./Maths/Fibonacci.php)
* [Fibonacci2](./Maths/Fibonacci2.php)
* [Greatestcommondivisor](./Maths/GreatestCommonDivisor.php)
* [Mean](./Maths/Mean.php)
* [Median](./Maths/Median.php)
* [Mode](./Maths/Mode.php)
Expand Down Expand Up @@ -58,6 +68,7 @@
* [Lowerbound](./Searches/LowerBound.php)
* [SentinelSearch](./Searches/SentinelSearch.php)
* [Ternarysearch](./Searches/TernarySearch.php)
* [Twopointers](./Searches/TwoPointers.php)
* [Upperbound](./Searches/UpperBound.php)


Expand All @@ -79,6 +90,7 @@
* [Checkpalindrome](./Strings/CheckPalindrome.php)
* [Checkpalindrome2](./Strings/CheckPalindrome2.php)
* [Countconsonants](./Strings/CountConsonants.php)
* [Counthomogenous](./Strings/CountHomogenous.php)
* [Countsentences](./Strings/CountSentences.php)
* [Countvowels](./Strings/CountVowels.php)
* [Distance](./Strings/Distance.php)
Expand All @@ -88,14 +100,20 @@

## Tests
* Ciphers
* [Atbashciphertest](./tests/Ciphers/AtbashCipherTest.php)
* [Cipherstest](./tests/Ciphers/CiphersTest.php)
* [Monoalphabeticciphertest](./tests/Ciphers/MonoAlphabeticCipherTest.php)
* [Morsecodetest](./tests/Ciphers/MorseCodeTest.php)
* [Railfenceciphertest](./tests/Ciphers/RailfenceCipherTest.php)
* [Vignereciphertest](./tests/Ciphers/VignereCipherTest.php)
* Conversions
* [Conversionstest](./tests/Conversions/ConversionsTest.php)
* Datastructures
* [Doublylinkedlisttest](./tests/DataStructures/DoublyLinkedListTest.php)
* [Singlylinkedlisttest](./tests/DataStructures/SinglyLinkedListTest.php)
* [Stacktest](./tests/DataStructures/StackTest.php)
* Graphs
* [Bellmanfordtest](./tests/Graphs/BellmanFordTest.php)
* [Breadthfirstsearchtest](./tests/Graphs/BreadthFirstSearchTest.php)
* [Depthfirstsearchtest](./tests/Graphs/DepthFirstSearchTest.php)
* Maths
Expand All @@ -106,7 +124,7 @@
* Sorting
* [Arraykeyssorttest](./tests/Sorting/ArrayKeysSortTest.php)
* [Gnomesorttest](./tests/Sorting/GnomeSortTest.php)
* [Sortingtests](./tests/Sorting/SortingTests.php)
* [Sortingtest](./tests/Sorting/SortingTest.php)
* Strings
* [Stringstest](./tests/Strings/StringsTest.php)

Expand Down
Loading

0 comments on commit f241d8d

Please sign in to comment.