Please take a look at the below code
def test
array = Array.new
array2 = Array.new
groups = [[424235, "goa", "italy"], [523436, "mumbai"], [342423, "africa", "goa"]]
type = ["goa", "mumbai"]
groups.each_with_index do |item,index|
if item.include?(type[0]) == true
array << index << array2
elsif item.include?(type[1]) == true
array2 << index
else
"nothing ;)"
end
end
print array.each_slice(2).map { |a, b| [a, b.first] }
end
combine
#Output - [[0, 1], [2, 1]]
See the problem with the code? That is I am using a bunch of if and else statements. What if type array has more than 2 entries. I cant go on writing the if and elsif statements. And thats where I need your help. What is a better what to structure the code? loops? if so how.
Here is my code.
Sample test cases
output :
[[0, 1], [2, 1]]output :
[[0, 2], [2, 2]]output :
[[0, 1, 3], [2, 1, 3]]output :
[[0, 1, 3, 0], [0, 2, 3, 0], [2, 1, 3, 0], [2, 2, 3, 0]]If I understood your problem correctly then these should be correct. Though I might have misunderstood you. Please do tell me If i have got your problem wrong, and If you could provide test cases that would be great.