Skip to content

Instantly share code, notes, and snippets.

@danielbonnell
Last active August 29, 2015 14:07
Show Gist options
  • Save danielbonnell/e9259c538c5921ae59a8 to your computer and use it in GitHub Desktop.
Save danielbonnell/e9259c538c5921ae59a8 to your computer and use it in GitHub Desktop.
morse_code
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