-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy path7_array.rb
More file actions
23 lines (22 loc) · 994 Bytes
/
7_array.rb
File metadata and controls
23 lines (22 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Given a sentence, return an array containing every other word.
# Punctuation is not part of the word unless it is a contraction.
# In order to not have to write an actual language parser, there won't be any punctuation too complex.
# There will be no "'" that is not part of a contraction.
# Assume each of these charactsrs are not to be considered: ! @ $ # % ^ & * ( ) - = _ + [ ] : ; , . / < > ? \ |
#
# Examples
# alternate_words("Lorem ipsum dolor sit amet.") # => ["Lorem", "dolor", "amet"]
# alternate_words("Can't we all get along?") # => ["Can't", "all", "along"]
# alternate_words("Elementary, my dear Watson!") # => ["Elementary", "dear"]
def alternate_words(sentence)
# this will get better when we learn regular expressions :)
'!@$#%^&*()-=_+[]:;,./<>?\\|'.split(//).each do |char|
sentence = sentence.gsub(char, ' ')
end
words = sentence.split
solution = []
words.each_with_index do |word, index|
solution << word if index.even?
end
solution
end