Last active
December 30, 2022 05:26
-
-
Save trejo08/8b63905e24297482f1149861479aa068 to your computer and use it in GitHub Desktop.
Morse Code translator written in Ruby as solution of CodementorX Assessment.
This file contains hidden or 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 |
Hi 👋 . Do you have this solution in Python?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Morse Code Translator
Solution written in
Ruby
for the below Morse Code Exercise inCodementorX Assessment
.Morse Code
Morse Code is delivered in a series signals which are referred to as dashes (
-
) or dots (.
).To keep things simple for the purposes of this challenge we'll only decode letters with a maximum length of three signals.
Here is the Morse Code dichotomic search table courtesy of Wikipedia
Morse Code Examples
-.-
translates toK
...
translates toS
.-
translates toA
--
translates toM
.
translates toE
Background
You've started work as morse code translator. Unfortunately some of the signals aren't as distinguishable as others and there are times where a
.
seems indistinguishable from-
.In these cases you write down a
?
so that you can figure out what all the posibilities of that letter for that word are later.Task
Write a function
possibilities
that will take a stringword
and return an array of possible characters that the morse codeword
could represent.Examples with
?
?
should return['E','T']
?.
should return['I','N']
.?
should return['I','A']
?-?
should return['R','W','G','O']