-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorse.rb
More file actions
28 lines (25 loc) · 814 Bytes
/
morse.rb
File metadata and controls
28 lines (25 loc) · 814 Bytes
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
def decode_char(character)
morse_dictionary = {
'.-' => 'A', '-...' => 'B', '-.-.' => 'C',
'..-.' => 'F', '.' => 'E', '-..' => 'D',
'--.' => 'G', '....' => 'H', '..' => 'I',
'.-..' => 'L', '-.-' => 'K', '.---' => 'J',
'--' => 'M', '-.' => 'N', '---' => 'O',
'.-.' => 'R', '--.-' => 'Q', '.--.' => 'P',
'...' => 'S', '-' => 'T', '..-' => 'U',
'-..-' => 'X', '.--' => 'W', '...-' => 'V',
'-.--' => 'Y', '--..' => 'Z'
}
morse_dictionary[character] || ' '
end
def decode_word(morse_word)
word = ''
morse_word.split.each do |char|
word += decode_char(char)
end
word
end
def decode(morse_message)
morse_message.split(' ').map { |word| decode_word(word) }.join(' ')
end
puts decode('.- -... --- -..- ..-. ..- .-.. .-.. --- ..-. .-. ..- -... .. . ...')