From: darren kirby Date: 2007-04-23T04:00:46+09:00 Subject: [solution][QUIZ] Morse Code (#121) Well, here's mine. Not pretty, but seems to do the job, though quite slowly. If you pass the script '--dict' after the code string it will check /usr/share/dict/words for matches (so I guess it will only work on Unix), and display them at the top of the results. This starts to take a while with all non-trivial code strings. I was considering an online dictionary for this, but gave it up after I realised the example given in the quiz itself produces over 5000 combinations. Not much error checking here either... The code: ############################################ code_key = { ".-" => "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" } WORDS = [] def recurse(ck, a) naa = [] a.each do |arr| 4.times do |n| if parse_chars(arr[2], ck, n+1) na = [arr[0] + ck.fetch(arr[2][0,n+1]), arr[1] \ + arr[2][0,n+1] + "|", arr[2][n+1..-1]] if na[2] == "" or na[2] == nil WORDS << "#{na[0]} => #{na[1][0..-2]}" if not \ WORDS.include?("#{na[0]} => #{na[1][0..-2]}") else if not naa.include?(na) naa << na end end end end end naa end def main(w, ck) wlen = w.length - 1 wa = [] wa << [ck.fetch(w[0,1]), w[0,1] + "|", w[1..-1]] if parse_chars(w, ck, 1) wa << [ck.fetch(w[0,2]), w[0,2] + "|", w[2..-1]] if parse_chars(w, ck, 2) wa << [ck.fetch(w[0,3]), w[0,3] + "|", w[3..-1]] if parse_chars(w, ck, 3) wa << [ck.fetch(w[0,4]), w[0,4] + "|", w[4..-1]] if parse_chars(w, ck, 4) wlen.times do |i| wa = recurse(ck, wa) end end def parse_chars(w, ck, n) if ck.has_key?(w[0,n]) true else false end end word_array = main(ARGV[0], code_key) if ARGV[1] == "--dict" a = IO.readlines("/usr/share/dict/words") WORDS.each do |w| if a.include?(w[0, w.index(' ')] + "\n") puts w WORDS.delete(w) end end puts end puts WORDS.sort ################################### -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972