-
Notifications
You must be signed in to change notification settings - Fork 68
/
StripComments.rb
47 lines (38 loc) · 885 Bytes
/
StripComments.rb
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
36
37
38
39
40
41
42
43
44
45
46
47
=begin
Complete the solution so that it strips all text that follows any of a set of
comment markers passed in. Any whitespace at the end of the line should also be
stripped out.
Example:
Given an input string of:
apples, pears # and bananas
grapes
bananas !apples
The output expected would be:
apples, pears
grapes
bananas
The code would be called like so:
solution("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"])
should == "apples, pears\ngrapes\nbananas"
=end
# My Solution
def solution(input, markers)
input = input.split("\n")
result = []
input.each do |x|
line = ""
0.upto(x.length-1) do |a|
if markers.include? x[a]
break
else
line += x[a]
end
end
result << line.strip
end
result.join("\n")
end
# Better Solution
def solution(input, markers)
input.gsub(/\s+[#{markers.join}].*/, "")
end