------_ extPart_001_01C78509.AEAF1E16
Content-Type: text/plain;
charset so-8859-1"
Content-Transfer-Encoding: quoted-printable
Below is my first solution to this quiz.
http://pastie.caboo.se/55736
- donald
# Ruby Quiz 121
# Donald A. Ball Jr.
# Version 1.0
class Morse
CODES = {
:A => '.-',
:N => '-.',
:B => '-...',
:O => '---',
:C => '-.-.',
:P => '.--.',
:D => '-..',
:Q => '--.-',
:E => '.',
:R => '.-.',
:F => '..-.',
:S => '...',
:G => '--.',
:T => '-',
:H => '....',
:U => '..-',
:I => '..',
:V => '...-',
:J => '.---',
:W => '.--',
:K => '-.-',
:X => '-..-',
:L => '.-..',
:Y => '-.--',
:M => '--',
:Z => '--..'
}
LETTERS = CODES.invert
def self.translate(cipher)
results = []
LETTERS.each_pair do |code, letter|
next unless cipher.slice(0, code.length) == code
if cipher.length == code.length
results << letter.to_s
else
translate(cipher.slice(code.length, cipher.length)).each do |result|
results << (letter.to_s << result)
end
end
end
results
end
end
Morse.translate(ARGV[0]).each{|word| puts word}
------_ extPart_001_01C78509.AEAF1E16--