Last active
August 29, 2015 14:07
-
-
Save danielbonnell/e9259c538c5921ae59a8 to your computer and use it in GitHub Desktop.
morse_code
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
def decode(code) | |
"SOLUTION GOES HERE" | |
key = { | |
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 => "----." | |
} | |
code = code.split(/(\s\/\s)|\s/) | |
decode = [] | |
newKey = key.invert | |
code.each do |c| | |
current = code.values_at(code.index(c)) | |
value = newKey.fetch(current) | |
if current == value | |
decode << value | |
end | |
end | |
decode = decode.join(' ') | |
puts decode | |
end | |
decode(".-.. .- ..- -. -.-. .... / .- -.-. .- -.. . -- -.-- / .. ... / .-.. --- -.-. .- - . -.. / .- - / ...-- ...-- / .... .- .-. .-. .. ... --- -. / .- ...- . .-.-.-") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment