Skip to content

@Jared-Prime /dictionary_kata.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
class DictionaryKata < Array
def initialize(word_size)
File.open("/usr/share/dict/words","r") do |words|
words.each do |word|
word.gsub!(/\n/,'')
self << word if word.size == word_size
end
end
end
def word_chain(word="cat", step=0)
case step
when 0
head = word[0]
tail = word[-1]
part = /^#{head}.#{tail}$/
when 1
head = word[0..1]
part = /^#{head}/
when 2
tail = word[-2..-1]
part = /#{tail}$/
end
self.select { |word| word.match(part) }
end
end
step = 0
node0 = "cat"
dictionary = DictionaryKata.new(3)
dictionary.word_chain(node0,step).each do |node1|
dictionary.word_chain(node1,step+1).each do |node2|
puts "%s => %s => %s => dog" % [node0, node1, node2] if dictionary.word_chain(node2,step+2).include?("dog")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.