-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathword_chains.rb
57 lines (48 loc) · 1.59 KB
/
word_chains.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class WordChains
def valid_dictionary(target_word)
words = []
File.readlines("2of12inf.txt").each do |line|
words << line.chomp if line.chomp.size == target_word.size && line.chomp != target_word
end
words
end
def adjacent_words(target_word, dictionary)
valid_words = []
target_word.length.times do |i|
("a".."z").each do |letter|
letter_diff_target = target_word.dup
letter_diff_target[i] = letter
if dictionary.include?(letter_diff_target) && !valid_words.include?(letter_diff_target)
valid_words << letter_diff_target
end
end
end
valid_words
end
def run(start_word, target_word)
word_sequence = {}
#add start_word's adjacent words
dictionary = valid_dictionary(target_word)
until word_sequence.has_key?(target_word)
next_level = store_words_in_hash(word_sequence)
next_level.merge(word_sequence)
end
#dict = valid_dictionary(first_word)
#word_to_change = first_word
end
def initial_population(start_word, dictionary)
end
def store_words_in_hash(storage_hash, dictionary)
#creates temporary storage hash for the next level of adjacent words
temporary_word_hash = {}
#iterate through all existing adj word pairs & find next level
storage_hash.keys.each do |parent|
#actually get adjacent words, assign to array, then put into hash
found_words = adjacent_words(parent, dictionary)
found_words.each do |adj_word|
temporary_word_hash[adj_word] = parent
end
end
temporary_word_hash
end
end