-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse_code.rb
69 lines (62 loc) · 1.63 KB
/
morse_code.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
57
58
59
60
61
62
63
64
65
66
67
68
69
MORSE_LOOKUP = {:'.-' => '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}
class MorseCode
attr_reader :encoded, :converted
def initialize(code)
@encoded = code
end
def convert_text
@converted = parse_words
end
def parse_words
encoded.split(' ').map { |x| x.split(' ')}
end
def decode_words
self.converted.map do |arr|
arr.map { |e| lookup(e) }.join()
end.join(' ')
end
def lookup(element)
::MORSE_LOOKUP.fetch(element.to_sym)
end
end
ARGF.each_line do |line|
morse_text = line.chomp
a = MorseCode.new(morse_text)
a.convert_text
puts a.decode_words
end