forked from adrianeyre/codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleSentence.rb
More file actions
35 lines (27 loc) · 775 Bytes
/
SimpleSentence.rb
File metadata and controls
35 lines (27 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
=begin
Implement a function, so it will produce a sentence out of the given parts.
Array of parts could contain:
- words;
- commas in the middle;
- multiple periods at the end.
Sentence making rules:
- there must always be a space between words;
- there must not be a space between a comma and word on the left;
- there must always be one and only one period at the end of a sentence.
Example:
make_sentence ['hello', ',', 'my', 'dear'] # returns 'hello, my dear.'
=end
# My Solution
def make_sentence parts
result = ""
parts.each do |x|
result += " " if x != "," && x != "."
result += x if x != "."
end
result += "."
result[1..result.length]
end
# Better Solution
def make_sentence parts
parts.join(' ').gsub(' ,', ',').sub(/(\s\.)*$/, '.');
end