3 ms·
Well, I can never resist implementing these interview questions! Here's my answer: def strip_dupes string res = '' string.each_char do |char| n
by saturn 15y ago
Well, I can never resist implementing these interview questions! Here's my answer:
def strip_dupes string
res = ''
string.each_char do |char|
next if char == res[-1]
res << char
end
res
end
Any improvements?
update: and can never resist reducing them to one-liners either.
def strip_dupes str
str.each_char.inject('') {|r,c| r << c unless c == r[-1]; r}
end
- nicpottier 15y agoYou're hired. Though we are mostly a Python shop, I'm sure you'll adapt. Sorry, no relocation package though. :)
- deleted 15y ago[deleted]
- candeira 15y agoYeah, I couldn't resist it either. Mine: def de_n_plicate(word): return ''.join(c for i, c in enumerate(word) if i == 0 or c != word[i-1])