From: Bob Lisbonne Date: 2007-04-23T14:20:52+09:00 Subject: Re: [QUIZ] Morse Code (#121) This is my first Ruby Quiz. I am relatively new to ruby, but have known morse code for a few years. :) Mine is a simple recursive algorithm using a regexp to match possibilities. I didn't bother adding the dictionary, or validating the user input, primarily because I liked the brevity of the solution below. I'd welcome any suggestion for making it more ruby-like. thanks, /Bob Lisbonne #!/usr/bin/env ruby -w class Morse @@cw = {'.-' => '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'} def initialize(dotsanddashes) parse(dotsanddashes,"") end def parse(dotsanddashes,letters) if dotsanddashes.size == 0 then puts letters return end @@cw.keys.each {|try| if /^#{try.gsub(/\./,'\.')}/.match(dotsanddashes) then parse(dotsanddashes[$&.size,dotsanddashes.size],letters + @@cw[$&]) end } end #parse end #Morse Morse.new(STDIN.read.chomp) On Apr 20, 2007, at 5:15 AM, Ruby Quiz 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 --.. >