-
-
Save jqn/832cadb51b31b30b14dcc10b6110e777 to your computer and use it in GitHub Desktop.
Morse Code translator written in Ruby as solution of CodementorX Assessment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Morse | |
def posibilities(signals) | |
signals.include?('?') ? check_wildcard(signals) : morses["#{signals}"] | |
end | |
def check_wildcard(signals) | |
length = signals.split('').length | |
values = [] | |
if length.eql?(1) | |
values = ["E", "T"] | |
else | |
indexes = [] | |
chars = signals.split('') | |
chars.each.with_index do |char, index| | |
next if char.eql?('?') | |
indexes << index | |
end | |
morses.keys.each do |morse| | |
next unless morse.length.eql?(length) | |
valid = true | |
indexes.each do |index| | |
next if chars[index].eql?(morse.split('')[index]) | |
valid = false | |
end | |
values << morses[morse] if valid | |
end | |
end | |
values | |
end | |
def morses | |
{ | |
'.' => 'E', | |
'-' => 'T', | |
'..' => 'I', | |
'.-' => 'A', | |
'-.' => 'N', | |
'--' => 'M', | |
'...' => 'S', | |
'..-' => 'U', | |
'.-.' => 'R', | |
'.--' => 'W', | |
'-..' => 'D', | |
'-.-' => 'K', | |
'--.' => 'G', | |
'---' => 'O' | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment