generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode_morse.rb
56 lines (51 loc) · 1011 Bytes
/
decode_morse.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
MORSE_MAP = {
'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' => '--..'
}.freeze
def decode_char(char)
MORSE_MAP.each do |key, value|
return key.upcase if value == char
end
end
def decode_word(word)
new_word = ''
new_arr = word.split
new_arr.each do |value|
new_word += decode_char(value)
end
new_word
end
def decode_morse_code(text)
new_sentence = ''
new_arr = text.split(' ')
new_arr.each do |value|
new_sentence += "#{decode_word(value)} "
end
new_sentence.strip
end
puts decode_morse_code('-- -.-- -. .- -- .')
puts decode_morse_code('.- -... --- -..- ..-. ..- .-.. .-.. --- ..-. .-. ..- -... .. . ...')