Morse code has been on my mind every now and then for the past several years. I don't write as much code as I used to since I left the software development industry, but today I decided it was time to write functions to encode and decode morse.
First, let's define an array to map the encodings for each character.
- $morse = array(
- " " => "/",
- "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" => "--..",
- "0" => "-----",
- "1" => ".----",
- "2" => "..---",
- "3" => "...--",
- "4" => "....-",
- "5" => ".....",
- "6" => "-....",
- "7" => "--...",
- "8" => "---..",
- "9" => "----.",
- "." => ".-.-.-",
- "," => "--..--",
- "?" => "..--..",
- "'" => ".----.",
- "!" => "-.-.--",
- "/" => "-..-.",
- "(" => "-.--.",
- ")" => "-.--.-",
- "&" => ".-...",
- ":" => "---...",
- ";" => "-.-.-.",
- "=" => "-...-",
- "+" => ".-.-.",
- "-" => "-....-",
- "_" => "..--.-",
- "\"" => ".-..-.",
- "$" => "...-..-",
- "@" => ".--.-.",
- );
This covers almost all possible morse characters, with the exception of accented letters.
Next, we need a function to encode messages.
- function morseEncodeMessage($message)
- {
- global $morse;
- $result = "";
- for ($i = 0; $i <= strlen($message) - 1; $i++)
- {
- $character = strtoupper(substr($message, $i, 1));
- $result .= (array_key_exists($character, $morse) ? $morse[$character] : "........") . " ";
- }
- $result = rtrim($result);
- return $result;
- }
We're looping through the message, one character at a time. Lowercase characters are turned into uppercase characters. We check the global array to see if there's a corresponding key, and if so we get the value associated with that key. If the character can't be found, we convert it to a series of eight dots to indicate that an error has occurred. Each encoded character is separated by a space; an extra trailing space is removed before we return the result. Spaces between words are turned into forward slashes.
Now we need a way of decoding messages.
- function morseDecodeMessage($message)
- {
- global $morse;
- $result = "";
- $characters = explode(" ", $message);
- foreach ($characters as &$character)
- {
- $result .= in_array($character, $morse, true) ? array_search($character, $morse, true) : "ERROR";
- }
- unset($character);
- return $result;
- }
We start by splitting the encoded message into an array, using the spaces between each letter as the separator. Then we loop through the local array and check the global array for each value. If the value is found, we retrieve the key, otherwise we indicate that an error has occurred.
Finally, let's write a unit test to make sure our code is working properly.
- function morseTest($message)
- {
- $testInput = $message;
- echo "Input: " . strtoupper($testInput) . "\n";
- $testEncoded = morseEncodeMessage($testInput);
- echo "Encoded: " . $testEncoded . "\n";
- $testDecoded = morseDecodeMessage($testEncoded);
- echo "Decoded: " . $testDecoded . "\n";
- echo "Result: Test " . ($testDecoded == strtoupper($testInput) ? "successful" : "failed") . "!\n";
- }
Nothing too complicated. Display the message before encoding, encode the message, display the encoded message, decode the message, display the decoded message, and compare the decoded message with the original.
If we execute the following:
morseTest("Hello world!");
We get this result:
Input: HELLO WORLD!
Encoded: .... . .-.. .-.. --- / .-- --- .-. .-.. -.. -.-.--
Decoded: HELLO WORLD!
Result: Test successful!
No comments:
Post a Comment