Skip to content

Instantly share code, notes, and snippets.

@Alex-Swann
Created March 10, 2016 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alex-Swann/06d4286b9c58c04813d5 to your computer and use it in GitHub Desktop.
Save Alex-Swann/06d4286b9c58c04813d5 to your computer and use it in GitHub Desktop.
$morse_dict = Hash[
"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" => "-----",
"SOS" => "...---...",
"sos" => "...---...",
"!" => "-.-.--",
"." => ".-.-.-"
]
def decodeMorse(morseCode)
words = []
morseCode.strip!
if morseCode.start_with? ".", "-"
arr = morseCode.split(" ")
arr.each{|x| words << x.split(" ")}
words.map!{|x| x.map{|y| $morse_dict.key(y)}}.map!(&:join)
return words.join(" ").upcase
else
arr = morseCode.downcase.split(" ")
arr.each{|x| words << x.split("")}
words.map!{|x| x.map{|y| $morse_dict[y]}}.map!{|x| x.join(" ")}
return words.join(" ")
end
end
puts "Please enter Morse Code to be decoded! Alternatively start with either a letter or number in order for your text to be converted to Morse."
puts decodeMorse(gets.chomp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment