Here's my solution.  Not much to it.  I didn't do any dictionary
stuff.
Pass -v to see all interpretations printed out.  Otherwise, just
number of interpretations printed.


#Author: Matt Hulse
#File  : morse.rb
#Email : matt.hulse / gmail.com
#Web   : http://www.matt-hulse.com

class Morse

	attr_reader :morse_code, :message, :result

	def initialize(message)
		@morse_code = { :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 => '--..'
		}
		@message = message
	end

	def translate
		@result = do_translation(@message).flatten
		puts "Translation Complete"
		puts "#{@result.length} interpretations found."
	end

	def do_translation(str)
		result = Array.new

		(1..4).each{|n|
			morse = str[0,n]
			this_char = decode(morse)
			if(this_char.nil?) then
				puts "Invalid char, skipping to next" if $DEBUG
				next
			else
				#is a valid character
				if(n == str.size)
					result << this_char
				elsif(n < str.size)
					result << do_translation(str[n,str.size]).flatten.collect{|c|
						this_char + c
					}
				end
			end
		}

		return result
	end

	def encode(char)
		encoded = ""
		if(char.size > 1) then
			char.split("").each{|letter|
				encoded += encode(letter) + "|"
			}
			encoded.chop!
		else
			result = @morse_code.find{|key,value| key == char.to_sym}
			if(result.nil?)
				return nil
			else
				encoded = result[1].to_s
			end
		end

		encoded
	end

	def decode(morse)
		result = @morse_code.find{|key,value| value == morse}
		if (not result.nil?) then
			result[0].to_s
		else
			return nil
		end
	end

	def show_result
		@result.each{|res|
			printf("#{encode(res)}%25s\n",res)
		}
	end
end


if __FILE__ == $0 then

	code = ARGV[0] ||= "...---..-....-"

	morse = Morse.new(code)
	morse.translate
	morse.show_result if $VERBOSE
end



Matt Hulse
matt.hulse / gmail.com
http://www.matt-hulse.com





On Apr 20, 6:15 am, Ruby Quiz <j... / grayproductions.net> wrote:
> The three rules of Ruby Quiz:
>
> 1.  Please do not post any solutions or spoiler discussion for this quiz until
> 48 hours have passed from the time on this message.
>
> 2.  Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rubyquiz.com/
>
> 3.  Enjoy!
>
> Suggestion:  A [QUIZ] in the subject of emails about the problem helps everyone
> on Ruby Talk follow the discussion.  Please reply to the original quiz message,
> if you can.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>
> The idea for this quiz was given to me by Yossef Mendelssohn.
>
> Morse code is a way to encode telegraphic messages in a series of long and short
> sounds or visual signals.  During transmission, pauses are used to group letters
> and words, but in written form the code can be ambiguous.
>
> For example, using the typical dot (.) and dash (-) for a written representation
> of the code, the word ...---..-....- in Morse code could be an encoding of the
> names Sofia or Eugenia depending on where you break up the letters:
>
>         ...|---|..-.|..|.-    Sofia
>         .|..-|--.|.|-.|..|.-  Eugenia
>
> This week's quiz is to write program that displays all possible translations for
> ambiguous words provided in code.
>
> Your program will be passed a word of Morse code on STDIN.  Your program should
> print all possible translations of the code to STDOUT, one translation per line.
> Your code should print gibberish translations in case they have some meaning for
> the reader, but indicating which translations are in the dictionary could be a
> nice added feature.
>
> We will only focus on the alphabet for this quiz to keep things simple.  Here
> are the encodings for each letter:
>
>         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 --..