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

ADD THE MORSE CODE MAP AND DECODER #1

Merged
merged 3 commits into from
Nov 3, 2023
Merged
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
54 changes: 54 additions & 0 deletions morse_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
@code_to_char = {
'.-' => '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'
}

def decode_char(morse_code)
@code_to_char[morse_code] || '?'
end

def decode_word(morse_word)
morse_code_to_words = morse_word.split
word = ''
morse_code_to_words.each do |i|
word += decode_char(i)
end
word
end

def decode_sentence(morse_sentence)
morse_code_to_sentence = morse_sentence.split(' ')
sentence = ''
morse_code_to_sentence.each do |w|
sentence += "#{decode_word(w)} "
end
sentence.chop
end

puts decode_sentence('.- -... --- -..- ..-. ..- .-.. .-.. --- ..-. .-. ..- -... .. . ...')

puts decode_sentence('-- -.-- -. .- -- .')
Loading