Skip to content

Instantly share code, notes, and snippets.

@kaecy
Created January 9, 2025 02:02
Show Gist options
  • Save kaecy/40d439fd9cf86dd1ee1bcddac722e252 to your computer and use it in GitHub Desktop.
Save kaecy/40d439fd9cf86dd1ee1bcddac722e252 to your computer and use it in GitHub Desktop.
Read and Write Morse Code
-- a lua program that encodes and decodes morse code messages. run it with the
-- command `lua marcy.lua decode`, followed by the morse code in quotes. it
-- translates the dots and dashes into plain text. translate plain text into
-- morse code using `lua marcy.lua encode`.
-- an example of the morse code it reads: -. . ...- . .-. / .-. ..- -.
-- use `lua marcy.lua decode "-. . ...- . .-. / .-. ..- -."` to decode it.
local morse_code_dict = {
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"] = "-----",
["."] = ".-.-.-", [","] = "--..--", ["?"] = "..--..", ["'"] = ".----.",
["!"] = "-.-.--", ["/"] = "-..-.", ["("] = "-.--.", [")"] = "-.--.-",
["&"] = ".-...", [":"] = "---...", [";"] = "-.-.-.", ["="] = "-...-",
["+"] = ".-.-.", ["-"] = "-....-", ["_"] = "..--.-", ['"'] = ".-..-.",
["$"] = "...-..-", ["@"] = ".--.-."
}
morse_code_decode_dict = {}
function encode(text_str)
encoded_text = ""
for i = 1, #text_str do
chr = text_str:sub(i , i)
if chr ~= " " then
if i ~= 1 then
encoded_text = encoded_text .. " "
end
encoded_text = encoded_text .. (morse_code_dict[chr])
else
encoded_text = encoded_text .. " /"
end
end
return encoded_text
end
function decode(text_str)
-- init decode table
if next(morse_code_decode_dict) == nil then
for letter, morse in pairs(morse_code_dict) do
morse_code_decode_dict[morse] = letter
end
end
decoded_text = ""
morse_buf = ""
for i = 1, #text_str do
chr = text_str:sub(i , i)
if chr ~= " " then
if chr == "/" then
if morse_buf ~= "" then
decoded_letter = morse_code_decode_dict[morse_buf]
if decoded_letter == nil then
return "error"
end
decoded_text = decoded_text .. decoded_letter
morse_buf = ""
end
decoded_text = decoded_text .. " "
else
morse_buf = morse_buf .. chr
end
else
if morse_buf ~= "" then
decoded_letter = morse_code_decode_dict[morse_buf]
if decoded_letter == nil then
return "error"
end
decoded_text = decoded_text .. decoded_letter
morse_buf = ""
end
end
end
if morse_buf ~= "" then
decoded_letter = morse_code_decode_dict[morse_buf]
if decoded_letter == nil then
return "error"
end
decoded_text = decoded_text .. decoded_letter
end
return decoded_text
end
if arg[1] == "encode" and arg[2] ~= nil then
print("Encoded: " .. encode(arg[2]))
end
if arg[1] == "decode" and arg[2] ~= nil then
print("Decoded: " .. decode(arg[2]))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment