mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
Adds Morse Code
This commit is contained in:
parent
3e8fd831f7
commit
74b93a53d0
@ -3,6 +3,7 @@
|
|||||||
## Ciphers
|
## Ciphers
|
||||||
* [Caesarcipher](./ciphers/caesarCipher.php)
|
* [Caesarcipher](./ciphers/caesarCipher.php)
|
||||||
* [Xorcipher](./ciphers/XORCipher.php)
|
* [Xorcipher](./ciphers/XORCipher.php)
|
||||||
|
* [Morse Code](./ciphers/morseCode.php)
|
||||||
|
|
||||||
## Conversions
|
## Conversions
|
||||||
* [Binarytodecimal](./Conversions/BinaryToDecimal.php)
|
* [Binarytodecimal](./Conversions/BinaryToDecimal.php)
|
||||||
|
119
ciphers/morseCode.php
Normal file
119
ciphers/morseCode.php
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode text to Morse Code.
|
||||||
|
* @param string text to encode
|
||||||
|
*/
|
||||||
|
function encode(string $text): string
|
||||||
|
{
|
||||||
|
$text = strtoupper($text); // Makes sure the string is uppercase
|
||||||
|
$MORSE_CODE = array( // Array containing morse code translations
|
||||||
|
"A" => ".-",
|
||||||
|
"B" => "-...",
|
||||||
|
"C" => "-.-.",
|
||||||
|
"D" => "-..",
|
||||||
|
"E" => ".",
|
||||||
|
"F" => "..-.",
|
||||||
|
"G" => "--.",
|
||||||
|
"H" => "....",
|
||||||
|
"I" => "..",
|
||||||
|
"J" => ".---",
|
||||||
|
"K" => "-.-",
|
||||||
|
"L" => ".-..",
|
||||||
|
"M" => "--",
|
||||||
|
"N" => "-.",
|
||||||
|
"O" => "---",
|
||||||
|
"P" => ".--.",
|
||||||
|
"Q" => "--.-",
|
||||||
|
"R" => ".-.",
|
||||||
|
"S" => "...",
|
||||||
|
"T" => "-",
|
||||||
|
"U" => "..-",
|
||||||
|
"V" => "...-",
|
||||||
|
"W" => ".--",
|
||||||
|
"X" => "-..-",
|
||||||
|
"Y" => "-.--",
|
||||||
|
"Z" => "--..",
|
||||||
|
"1" => ".----",
|
||||||
|
"2" => "..---",
|
||||||
|
"3" => "...--",
|
||||||
|
"4" => "....-",
|
||||||
|
"5" => ".....",
|
||||||
|
"6" => "-....",
|
||||||
|
"7" => "--...",
|
||||||
|
"8" => "---..",
|
||||||
|
"9" => "----.",
|
||||||
|
"0" => "-----",
|
||||||
|
" " => "/"
|
||||||
|
);
|
||||||
|
|
||||||
|
$encodedText = ""; // Stores the encoded text
|
||||||
|
foreach (str_split($text) as $c) { // Going through each character
|
||||||
|
if (array_key_exists($c, $MORSE_CODE)) { // Checks if it is a valid character
|
||||||
|
$encodedText .= $MORSE_CODE[$c] . " "; // Appends the correct character
|
||||||
|
} else {
|
||||||
|
throw new \Exception("Invalid character: $c");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
substr_replace($encodedText ,"", -1); // Removes trailing space
|
||||||
|
return $encodedText;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode Morse Code to text.
|
||||||
|
* @param string text to decode
|
||||||
|
*/
|
||||||
|
function decode(string $text): string
|
||||||
|
{
|
||||||
|
$MORSE_CODE = array( // An array containing morse code to text translations
|
||||||
|
".-" => "A",
|
||||||
|
"-..." => "B",
|
||||||
|
"-.-." => "C",
|
||||||
|
"-.." => "D",
|
||||||
|
"." => "E",
|
||||||
|
"..-." => "F",
|
||||||
|
"--." => "G",
|
||||||
|
"...." => "H",
|
||||||
|
".." => "I",
|
||||||
|
".---" => "J",
|
||||||
|
"-.-" => "K",
|
||||||
|
".-.." => "L",
|
||||||
|
"--" => "M",
|
||||||
|
"-." => "N",
|
||||||
|
"---" => "O",
|
||||||
|
".--." => "P",
|
||||||
|
"--.-" => "Q",
|
||||||
|
".-." => "R",
|
||||||
|
"..." => "S",
|
||||||
|
"-" => "T",
|
||||||
|
"..-" => "U",
|
||||||
|
"...-" => "V",
|
||||||
|
".--" => "W",
|
||||||
|
"-..-" => "X",
|
||||||
|
"-.--" => "Y",
|
||||||
|
"--.." => "Z",
|
||||||
|
".----" => "1",
|
||||||
|
"..---" => "2",
|
||||||
|
"...--" => "3",
|
||||||
|
"....-" => "4",
|
||||||
|
"....." => "5",
|
||||||
|
"-...." => "6",
|
||||||
|
"--..." => "7",
|
||||||
|
"---.." => "8",
|
||||||
|
"----." => "9",
|
||||||
|
"-----" => "0",
|
||||||
|
"/" => " "
|
||||||
|
);
|
||||||
|
|
||||||
|
$decodedText = ""; // Stores the decoded text
|
||||||
|
foreach (explode(" ", $text) as $c) { // Going through each group
|
||||||
|
if (array_key_exists($c, $MORSE_CODE)) { // Checks if it is a valid character
|
||||||
|
$decodedText .= $MORSE_CODE[$c]; // Appends the correct character
|
||||||
|
} else {
|
||||||
|
if ($c) { // Makes sure that the string is not empty to prevent trailing spaces or extra spaces from breaking this
|
||||||
|
throw new \Exception("Invalid character: $c");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $decodedText;
|
||||||
|
}
|
15
tests/MorseCodeTest.php
Normal file
15
tests/MorseCodeTest.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use function PHPUnit\Framework\assertEquals;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
require_once __DIR__ . '/../ciphers/morseCode.php';
|
||||||
|
|
||||||
|
class CiphersTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testCaesarCipher()
|
||||||
|
{
|
||||||
|
assertEquals('Test 123', decrypt(encrypt('Test 123')));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user