Last active
December 26, 2015 19:39
-
-
Save hauntedhost/7203068 to your computer and use it in GitHub Desktop.
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
# 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