Skip to content

Instantly share code, notes, and snippets.

@hauntedhost
Last active December 26, 2015 19:39
Show Gist options
  • Save hauntedhost/7203068 to your computer and use it in GitHub Desktop.
Save hauntedhost/7203068 to your computer and use it in GitHub Desktop.
# Build a function morse_encode that takes in a word (no numbers or
# punctuation) and outputs the morse code for it. Put two spaces between
# words and one space between letters.
# http://www.w1wc.com/pdf_files/international_morse_code.pdf
def morse_encode(str)
morse_codes = { 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: "--.."
}
morse_result = []
str.split(" ").each do |word|
morse_result << word.chars.map { |c| morse_codes[c.to_sym] }.join(" ")
end
morse_result.join(" ")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment