Morse code day - April 27th

Hello,

Morse code day was April 27th https://www.daysoftheyear.com/days/morse-code-day/

I noticed no package for morse code seems to exist with Julia

Here is my small (and late) contribution

module MorseCode

using OrderedCollections: OrderedDict

const ALPHANUMERIC_TO_MORSE = OrderedDict(
    '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' => "-----",

    '&' => ".-...", '\'' => ".----.", '@' => ".--.-.", ')' => "-.--.-", '(' => "-.--.",
    '⇒' => "---...", ',' => "--..--", '=' => "-...-", '!' => "-.-.--", '.' => ".-.-.-",
    '-' => "-....-", '+' => ".-.-.", '?' => "..--..", '/' => "-..-."
)

const MORSE_TO_ALPHANUMERIC = Dict(value => key for (key, value) in ALPHANUMERIC_TO_MORSE)

function encode(sentence)
    sentence = uppercase(sentence)
    morse_code = String[]
    for c in sentence
        if c == ' '
            push!(morse_code, " ")
        else
            push!(morse_code, ALPHANUMERIC_TO_MORSE[c])
        end
    end
    return join(morse_code, " ")
end

function decode(morse_code)
    morse_code = split(morse_code, " ")
    sentence = String[]
    for morse_symbol in morse_code
        if morse_symbol == ""
            push!(sentence, " ")
        else
            push!(sentence, string(MORSE_TO_ALPHANUMERIC[morse_symbol]))
        end
    end
    return replace(join(sentence), "  " => " ")
end


end # module
julia> decode(".... . .-.. .-.. ---   .--- ..- .-.. .. .-   -.-.--")

Maybe we can expect to have the current situation improved for next year :wink:

I’m pretty sure nice projects could be done in this field using Julia

Kind regards

8 Likes
--. --- --- -.. / ... ..- --. --. . ... - .. --- -. -.-.--
1 Like

Some ideas…

-... ..- .. .-.. -..   .-   .--- ..- .-.. .. .-   .--. .- -.-. -.- .- --. .   ..-. --- .-.   . -. -.-. --- -.. .. -. --.   -.. . -.-. --- -.. .. -. --.   
- . -..- -   - ---   -- --- .-. ... .   .- -. -..   -- --- .-. ... .   - ---   - . -..- - --..--   -.-. .-. . .- - .   -- --- .-. ... .   -... .. -. .- .-. -.--   - .-. . .   -.--. -.. .. -.-. .... --- - --- -- .. -.-.   ... . .- .-. -.-. ....   - .- -... .-.. . -.--.- --..--   -.-. .-. . .- - .   .-   .--- ..- .-.. .. .-   ... -.-. .-. .. .--. -   ... .. -- .. .-.. .- .-.   - ---   -- --- .-. ... . .-.-.- .--. -.--   ..-. .-. --- --   -.- .- --. --. .-.. .   -- --- .-. ... .   -.-. .... .- .-.. .-.. . -. --. .   - ---   .-.. .. ... - . -.   .- -. -..   -.. .. ... .--. .-.. .- -.--   .-- .- ...-   -- --- .-. ... .   -.-. --- -.. .   ..-. .. .-.. . --..--   -.-. .-. . .- - .   .--- ..- .-.. .. .-   -.-. --- -.. .   - ---   -.. . -.-. --- -.. .   .-- .- ...-   -- --- .-. ... .   ..-. .. .-.. .   .- -. -..   -... ..- .. .-.. -..   .-   .--- ..- .-.. .. .-   .-. . .- .-..   - .. -- .   -.. . -.-. --- -.. . .-.   .- .-. .   ... --- -- .   ... ..- --. --. . ... - .. --- -. ... .-.-.-   -.- . . .--.   -- .   .. -. ..-. --- .-. -- . -.. -.-.--