Skip to content

Commit

Permalink
Extract general method to find n-word phrases
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryb committed Jul 15, 2009
1 parent 33e4d64 commit 813d40d
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions 1.rb
Expand Up @@ -16,22 +16,20 @@ def phrases(number)
private

def double_words(strings)
all_strings = []

# Extracting double-word phrases
(0...strings.size - 1).each do |i|
all_strings << "#{strings[i]} #{strings[i + 1]}"
end
return all_strings
extract_phrases(strings, 2)
end

def triple_words(strings)
all_strings = []

(0...strings.size - 2).each do |i|
all_strings << "#{strings[i]} #{strings[i + 1]} #{strings[i + 2]}"
extract_phrases(strings, 3)
end

def extract_phrases(strings, number_of_words)
result = []
(0...strings.size - number_of_words + 1).each do |i|
phrase = strings[i, number_of_words].join(' ')
result << phrase
end
return all_strings
result
end
end

Expand Down

0 comments on commit 813d40d

Please sign in to comment.