Skip to content

Commit

Permalink
No need to calculate single words when we already have them
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryb committed Jul 15, 2009
1 parent e6cba04 commit 04b2a46
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions 1.rb
Expand Up @@ -7,25 +7,19 @@
module StringExtensions
def phrases(number)
words = split
all_strings = single_double_triple_words(words)
all_strings = words
all_strings += double_triple_words(words)
all_strings[0, number].map {|s| "'#{s}'"}.join(', ')
end

private

def single_double_triple_words(strings)
def double_triple_words(strings)
all_strings = []
num_words = strings.size

return all_strings unless has_enough_words(num_words)

# Extracting single words. Total size of words == num_words

# Extracting single-word phrases.
(0...num_words).each do |i|
all_strings << strings[i]
end

# Extracting double-word phrases
(0...num_words - 1).each do |i|
all_strings << "#{strings[i]} #{strings[i + 1]}"
Expand Down

0 comments on commit 04b2a46

Please sign in to comment.