Created
January 13, 2013 23:46
-
-
Save kennyt/4526878 to your computer and use it in GitHub Desktop.
This file contains 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
class Word | |
# this could be changed to attr_reader since you only use it to read the value | |
attr_accessor :word, :parent_word_obj | |
def initialize(word, parent_word_obj) | |
@word = word | |
@parent_word_obj = parent_word_obj | |
end | |
end | |
class WordChains | |
def initialize(first_word, last_word) | |
@dictionary_array = [] | |
@possible_words = [] | |
@first_word = Word.new(first_word, nil) | |
@last_word = Word.new(last_word, nil) | |
@total_word_object_array = [] | |
end | |
def word_chains | |
read_dictionary | |
reduce_possible_words | |
last_word_parent = run_adjacent_words(@first_word) | |
if (last_word_parent == nil) | |
puts "Sorry, no path between #{@first_word.word} and #{@last_word.word}." | |
else | |
path = build_path(last_word_parent) | |
#should put the print_array_of_words method here | |
puts path.reverse.join(", ") | |
end | |
end | |
def read_dictionary | |
File.foreach('2of12inf.txt') do |word| | |
@dictionary_array << word.chomp.chomp('%') | |
end | |
end | |
def reduce_possible_words | |
# you can take @possible_words out completely by just re-setting | |
# @dictionary_array here. @dictionary_array = @dictionary_array.select {etc.} | |
@possible_words = @dictionary_array.select {|word| word.length == @first_word.word.length} | |
end | |
def adjacent_words(word_object) | |
adjacent_word_object_array = [] | |
index = 0 | |
while index < word_object.word.length do # <-- this "do" is uneccessary | |
("a".."z").each do |letter| | |
new_word = word_object.word.dup | |
new_word[index] = letter | |
if @possible_words.include?(new_word) && new_word != word_object.word ##&& !adjacent_word_object_array.any? {|word_object_item| word_object_item.word == new_word} | |
adjacent_word_object_array << Word.new(new_word, word_object) | |
end | |
end | |
index += 1 | |
end | |
adjacent_word_object_array | |
end | |
# method name should be : find_last_word_parent | |
# you can completely take out the (word_object) argument by calling | |
# the class instance @first_word directly. you can take advantage of the | |
# class variable, after all :) | |
def run_adjacent_words(word_object) | |
# @total_word_object_array << @first_word; would be the same thing | |
@total_word_object_array << word_object | |
i = 0 | |
until @total_word_object_array.any? {|word_object_item| word_object_item.word == @last_word.word} | |
if @total_word_object_array[i] == nil | |
return nil | |
end | |
new_adjacent_words = adjacent_words(@total_word_object_array[i]) | |
## push new words onto the array if they are not already there | |
new_adjacent_words.each do |word_obj| | |
# you can use "unless" instead of the "if !" for better readability | |
if !(@total_word_object_array.any? {|word_obj_item| word_obj_item.word == word_obj.word }) | |
@total_word_object_array << word_obj | |
end | |
end | |
last_word_parent = @total_word_object_array[i] | |
i += 1 | |
end | |
last_word_parent | |
end | |
def build_path(last_word_parent) | |
path = [@last_word.word] | |
until last_word_parent.parent_word_obj == nil | |
path << last_word_parent.word | |
last_word_parent = last_word_parent.parent_word_obj | |
end | |
path << @first_word.word | |
path | |
end | |
# this method was never used. it is a nice decoration, though | |
def print_array_of_words(array) | |
array.each do |word_obj| | |
puts "#{word_obj.word} => #{word_obj.parent_word_obj.word}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment