Last active
March 20, 2020 02:46
-
-
Save vxzry/a19ba3132bebd4b2e5a51fb82137a6bc to your computer and use it in GitHub Desktop.
PHP code to convert morse code into english text and vice versa - challenge from https://github.com/ponchog/phpcodingchallenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Converts morse code into english text and vice versa | |
* php version 7.2 | |
* | |
* @category NA | |
* @package NA | |
* @author vzxry <[email protected]> | |
*/ | |
namespace MorseConverter; | |
class MorseConverter | |
{ | |
const MORSECODES = array( | |
array('char'=>'0', 'morse'=>"-----"), | |
array('char'=>'1', 'morse'=>".----"), | |
array('char'=>'2', 'morse'=>"..---"), | |
array('char'=>'3', 'morse'=>"...--"), | |
array('char'=>'4', 'morse'=>"....-"), | |
array('char'=>'5', 'morse'=>"....."), | |
array('char'=>'6', 'morse'=>"-...."), | |
array('char'=>'7', 'morse'=>"--..."), | |
array('char'=>'8', 'morse'=>"---.."), | |
array('char'=>'9', 'morse'=>"----."), | |
array('char'=>'A', 'morse'=>".-"), | |
array('char'=>'B', 'morse'=>"-..."), | |
array('char'=>'C', 'morse'=>"-.-."), | |
array('char'=>'D', 'morse'=>"-.."), | |
array('char'=>'E', 'morse'=>"."), | |
array('char'=>'F', 'morse'=>"..-."), | |
array('char'=>'G', 'morse'=>"--."), | |
array('char'=>'H', 'morse'=>"...."), | |
array('char'=>'I', 'morse'=>".."), | |
array('char'=>'J', 'morse'=>".---"), | |
array('char'=>'K', 'morse'=>"-.-"), | |
array('char'=>'L', 'morse'=>".-.."), | |
array('char'=>'M', 'morse'=>"--"), | |
array('char'=>'N', 'morse'=>"-."), | |
array('char'=>'O', 'morse'=>"---"), | |
array('char'=>'P', 'morse'=>".--."), | |
array('char'=>'Q', 'morse'=>"--.-"), | |
array('char'=>'R', 'morse'=>".-."), | |
array('char'=>'S', 'morse'=>"..."), | |
array('char'=>'T', 'morse'=>"-"), | |
array('char'=>'U', 'morse'=>"..-"), | |
array('char'=>'V', 'morse'=>"...-"), | |
array('char'=>'W', 'morse'=>".--"), | |
array('char'=>'X', 'morse'=>"-..-"), | |
array('char'=>'Y', 'morse'=>"-.--"), | |
array('char'=>'Z', 'morse'=>"--.."), | |
array('char'=>".", 'morse'=>".-.-.-"), | |
array('char'=>",", 'morse'=>"--..--"), | |
array('char'=>"?", 'morse'=>"..--.."), | |
array('char'=>"!", 'morse'=>"-.-.--"), | |
array('char'=>"/", 'morse'=>"-..-."), | |
array('char'=>"(", 'morse'=>"-.--.-"), | |
array('char'=>")", 'morse'=>"-.--.-"), | |
array('char'=>"&", 'morse'=>".-..."), | |
array('char'=>":", 'morse'=>"---..."), | |
array('char'=>";", 'morse'=>"-.-.-."), | |
array('char'=>"=", 'morse'=>"-...-"), | |
array('char'=>"+", 'morse'=>".-.-."), | |
array('char'=>"-", 'morse'=>"-....-"), | |
array('char'=>"_", 'morse'=>"..--.-"), | |
array('char'=>"\"",'morse'=>".-..-."), | |
array('char'=>"$", 'morse'=>"...-..-"), | |
array('char'=>"@", 'morse'=>".--.-.")); | |
/** | |
* Converts input into Morse code | |
* | |
* @param $input string | |
* | |
* @return string output | |
*/ | |
public static function toMorse($input) | |
{ | |
$morse = array_column(MorseConverter::MORSECODES, 'char', 'morse'); | |
$result = ''; | |
forEach (str_split($input) as $char) { | |
$morseSearch = array_search(strtoupper($char), $morse); | |
if ($morseSearch) { | |
$result = $result.$morseSearch.' '; | |
} | |
if ($char == ' ') { | |
$result = $result.'/'; | |
} | |
} | |
return $result; | |
} | |
/** | |
* Converts input from Morse code to English text | |
* | |
* @param $input string | |
* | |
* @return string output | |
*/ | |
public static function fromMorse($input) | |
{ | |
$morse = array_column(MorseConverter::MORSECODES, 'morse', 'char'); | |
$result = ''; | |
forEach (explode(' ', $input) as $char) { | |
if ($char != '') { | |
if (substr($char[0], 0, 1) == '/') { | |
$result = $result.' '; | |
$char = substr($char, 1); | |
} | |
$morseSearch = array_search($char, $morse); | |
if ($morseSearch) { | |
$result = $result.$morseSearch; | |
} | |
} | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: Comments hahaha