-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorse_code.rb
More file actions
32 lines (25 loc) · 746 Bytes
/
morse_code.rb
File metadata and controls
32 lines (25 loc) · 746 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
29
30
31
32
ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.freeze
MORSECODE = [
'.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....',
'..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.',
'--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-',
'-.--', '--..'
].freeze
def decode_char(char)
ABC[MORSECODE.find_index(char)]
end
def decode_word(word)
separate = word.split
full_word = []
separate.each { |letter| full_word.append(decode_char(letter)) }
full_word.join
end
def decode(sentence)
separate = sentence.split(' ')
full_sentence = []
separate.each do |word|
full_sentence.append(decode_word(word))
end
full_sentence.join(' ')
end
p decode('.- -... --- -..- ..-. ..- .-.. .-.. --- ..-. .-. ..- -... .. . ...')