Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extract general method to find n-word phrases

  • Loading branch information...
commit 813d40d060778bfb8e93c302870d18d6d8c7d1ef 1 parent 33e4d64
@kerryb authored
Showing with 10 additions and 12 deletions.
  1. +10 −12 1.rb
View
22 1.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.