Created
April 3, 2013 03:24
-
-
Save msonnabaum/5298208 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
# Kata Eight: Conflicting Objectives | |
# by Mark, Mark, and Lance | |
def words | |
@words ||= File.read('./wordsEn.txt').split("\r\n") | |
end | |
def six_letter_words(words) | |
words.reject do |word| | |
word.length != 6 | |
end | |
end | |
def find_less_than_six_letter_words(words) | |
words.each_with_object({}) do |word, memo| | |
memo[word] = true if word.length < 6 | |
end | |
end | |
def has_two_words?(word, word_list) | |
word1_last_index_range = 1..3 | |
word2_last_char_index = 5 | |
word1_last_index_range.to_a.each do |word1_last_index| | |
word1 = word[0..word1_last_index] | |
word2 = word[word1_last_index+1..word2_last_char_index] | |
return true if word_list[word1] and word_list[word2] | |
end | |
false | |
end | |
def words_containing_two_words(word_list) | |
words_that_contain_two_words = [] | |
less_than_six_letter_words = find_less_than_six_letter_words(word_list) | |
six_letter_words(word_list).each do |word| | |
if has_two_words?(word, less_than_six_letter_words) | |
words_that_contain_two_words << word | |
end | |
end | |
words_that_contain_two_words | |
end | |
start_time = Time.now | |
num_words = words_containing_two_words(words).length | |
elapsed = Time.now - start_time | |
raise "Incorrect!" unless num_words == 3715 | |
puts "#{num_words} found in #{elapsed*1000}ms" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment