Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Morse code decoder has been done #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions decoder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env ruby

morse = {
"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" => "−−−−−",
" " => " ",
"." => ".−.−.−",
"," => "−−..−−",
"?" => "..−−..",
"'" => ".−−−−.",
"!" => "−.−.−−",
"/" => "−..−.",
"(" => "−.−−.",
")" => "−.−−.−",
"&" => ".−...",
":" => "−−−...",
";" => "−.−.−.",
"=" => "−...−",
"+" => ".−.−.",
"-" => "−....−",
"_" => "..−−.−",
"\"" => ".−..−.",
"$" => "...−",
}

answer = ARGV.first

message = STDIN.gets.chomp.upcase

if answer == "-e"
output = message.split("").map do |letter|
morse[letter] + " "
end.join("")
puts output
elsif answer == "-d"
output = message.split(" ").map do |word|
morse.key(word)
end.join("")
puts output
else
STDERR.puts "-manual-"
STDERR.puts "encode: -e"
STDERR.puts "decode: -d"
STDERR.puts "example: echo \"hello\" | ruby program.rb -e"
end