On 4/20/07, Ruby Quiz <james / grayproductions.net> wrote: > > 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. > This is my first Ruby Quiz. I am interested in seeing how it can be made to run faster and also how other people approached it. ##### letters = Hash.new("*") abc = %w[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] marks = [".-","-...","-.-.","-..",".","..-.","--.","....", "..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.", "...","-","..-","...-",".--","-..-","-.--","--.."] marks.each do |x| letters.store(x,abc[marks.index(x)]) end puts "Enter Morse code" str = gets.chomp str_arr = str.split(//) nums = [] (0..5 ** str.length/4).each do |b| if b.to_s(5) !~ /0/ sum = 0 b.to_s(5).split(//).each {|hj| sum += hj.to_i } if sum == str.length nums << b.to_s(5) end end end unpackers = [] nums.each do |x| unpackers << x.to_s.split(//).collect {|u| "A" + u}.join end morse = [] unpackers.each do |g| morse << str.unpack(g) end words = [] morse.each do |t| word = "" t.each do |e| word << letters[e] end words << word unless word =~ /\*/ end puts words ### Harry -- http://www.kakueki.com/ruby/list.html A Look into Japanese Ruby List in English