Skip to content

Commit

Permalink
ADD THE MORSE CODE MAP AND DECODER
Browse files Browse the repository at this point in the history
  • Loading branch information
Ochiengsteven committed Nov 3, 2023
1 parent f22768e commit 6804cc2
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions morse_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@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 |j|
sentence += "#{decode_word(j)} "
end
sentence.chop
end

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

0 comments on commit 6804cc2

Please sign in to comment.