-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.rb
25 lines (21 loc) · 1.01 KB
/
decoder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
MORSE_CODE = {
'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' => '--..', '0' => '-----', '1' => '.----', '2' => '..---', '3' => '...--', '4' => '....-',
'5' => '.....', '6' => '-....', '7' => '--...', '8' => '---..', '9' => '----.'
}.freeze
def decode_character(character)
MORSE_CODE.each { |key, value| return key if value == character }
end
def decode_word(word)
result = ''
word.split.each { |character| result += decode_character(character) }
result
end
def decode_message(message)
result = []
message.strip.split(' ').each { |word| result.append(decode_word(word)) }
result.join(' ')
end
puts decode_message(' .- -... --- -..- ..-. ..- .-.. .-.. --- ..-. .-. ..- -... .. . ...')