Reputation: 6394
I have the following array:
str_ary = ["Thursday morning", "Twitter users", "Thursday morning , 140 characters",
"of Twitter users", "the virtual pockets of Twitter users","Beginning Thursday morning , 140 characters","in the virtual pockets of Twitter users"]
I want to filter it and get str_ary2 = ["Thursday morning", "Twitter users"].
Also if there is a unique string there (that is not part of any of other string, I want to keep it as well..).
What would be the best way to do it?
Now I have this, but it does not work...
def select_correct_sizes(arrays)
result = []
arrays.each do |a|
arrays.each do |b|
res = nil
if b != a
if a.split(' ').length >= b.split(' ').length
res = self.substract_and_check(a, b)
elsif a.split(' ').length < b.split(' ').length
res = self.substract_and_check(b, a)
end
if !res.nil?
result << res
end
end
end
end
result = result.uniq
return result
end
def substract_and_check(a, b)
res = a.gsub(/#{b}/, '')
res = res.split(' ')
if res.length + b.split(' ').length == a.split(' ').length
puts "#{b} IS PART OF THE #{a}"
return b
elsif text_uniq?(a,b)
puts "#{b} IS UNIQUE"
return b
else
return nil
end
end
def text_uniq?(a,b)
res = a.gsub(/#{b}/, '')
res = res.split(' ')
if res.length == a.split(' ').length
return true
else
return false
end
end
str_ary2 = select_correct_sizes(str_ary)
EDIT: Sorry if the question is not quite clear.. I need to extract strings, that are
A) 1)present in other strings from array 2)smallest in size B) 1) Unique (e.g. not present in any other strings from the array).
All the strings are filtered phrases, so there won't be anything like random individual junk words like "the", "one" etc..
In the above example "Twitter users" and "Thursday morning" are both present in other strings from the array.
So if the array included something like "green ball", I need to extract it as well, because it is unique relative to other strings in the array.
Hope it is more clear now, please let me know otherwise.
EDIT2: I don't expect anyone to use the above code for answer, I will accept a different code or a well-detailed pseudo-code as well..
Upvotes: 0
Views: 69
Reputation: 44080
If I understood the question correctly, you want all the elements which don't include any other element.
str_ary = ["Thursday morning", "Twitter users", "Thursday morning , 140 characters",
"of Twitter users", "the virtual pockets of Twitter users",
"Beginning Thursday morning , 140 characters","in the virtual pockets of Twitter users",
'green ball']
str_ary.reject{|e| (str_ary - [e]).any?{|e1| e.include?(e1)}}
# => ["Thursday morning", "Twitter users", "green ball"]
Upvotes: 2