RB
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
=begin
Challenge: remove duplicate words from a given string, but there s a catch
Rules
-remove all duplicate words from the string , preserving first occurrence
-note that "so" and "so," are duplicates too, but comma or any grammatical sign must be preserved at all times but not the duplicate word
sample text- "so we started so, we start again the gain, we! never give up up! up;"
o/p:" so we started , start again the gain, ! never give up ! ;"
=end
input = "so we started so, we start again the gain, we! never give up up! up;"
def impl1(input)
tmp = input.gsub(/(\w*)(\W)/, '\1 \2').split(' ')
answer = tmp.reduce([]) {|memo, x|
if memo.include?(x) == false or x.match(/\W/)
memo << x
else
memo
end
}.join(' ')
end
def impl2(input)
tmp = input.split(' ').reduce([]) { |m,x|
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run