Last active
August 29, 2015 14:03
-
-
Save omerisimo/939e604ef4f61e455d1b to your computer and use it in GitHub Desktop.
A solution to the Ruby Quiz: http://rubyquiz.com/quiz121.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################# | |
# A solution to the Ruby Quiz: http://rubyquiz.com/quiz121.html # | |
################################################################# | |
INPUT = "...---..-....-" # Eugenia or Sofia | |
HELLO = "......-...-..---" # Hello | |
OK = "----.-" # Ok or Toa | |
class Morse | |
LETTERS_REPRESENTATION = { | |
"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" => "--.." | |
} | |
MORSE_TO_LETTERS = LETTERS_REPRESENTATION.invert | |
# Returns an array of all possible decoded permutations of the input morse code | |
def self.words(code) | |
words = [] | |
# Decode the first letter (which might be 1-4 charecters long) | |
# and recursively parse the rest of the input | |
possible_first_letters(code) do |letter, length| | |
remeaning = words(code[length+1..-1]) | |
possible_words(letter, remeaning) do |word| | |
words.push word | |
end | |
end | |
words | |
end | |
private | |
def self.possible_first_letters(input) | |
max_letter_length = [3, input.length-1].min | |
for length in 0..max_letter_length do | |
letter = MORSE_TO_LETTERS[input[0..length]] | |
yield letter, length if letter | |
end | |
end | |
def self.possible_words(first_letter, remeaning) | |
if remeaning.empty? | |
yield first_letter | |
else | |
remeaning.each do |letters| | |
yield first_letter + letters | |
end | |
end | |
end | |
end | |
class Dictionary | |
DICTIONARY_FILE = '/usr/share/dict/words' | |
def self.preload | |
# Build a hash of dictionary words from the dictionary file | |
@@dict ||= Hash[File.readlines(DICTIONARY_FILE).map {|word| [word.strip.downcase, true]}] | |
end | |
def self.include?(word) | |
# If the dictioary was not initialized search the file | |
return open(DICTIONARY_FILE) { |f| f.grep(/^#{word.downcase}$/i) }.any? unless @@dict | |
@@dict[word.downcase] | |
end | |
end | |
class BoredClerk | |
def initialize | |
puts "I am busy! Please wait while I read a bit from the dictionary... Ohh how I love to learn new words!" | |
Dictionary.preload | |
puts "Yes, how may I help you?" | |
end | |
def read_morse(code) | |
repsond words(code) | |
end | |
private | |
def repsond(words) | |
if words.empty? | |
puts "This Gibberish doesn't make any sense!" | |
elsif words.length == 1 | |
puts "Your code says '#{words.first}'" | |
else | |
puts "Your code might mean #{words.join(' or ')}." | |
end | |
end | |
def words(code) | |
Morse.words(code).map { |word| word if Dictionary.include? word }.compact | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment