I am trying to solve a problem where I need to find the airport code in an array of arrays of that represents the starting point of a multi-city flight plan. For example: Given
I see what you're attempting. The originating airport would be the only one included in a 0 index of a sub array but not a 1 index of any sub array.
However, in you're solution, you are comparing each combination of 2 sub arrays (including a sub array and itself, by the way...), and then returning the index of list
if a[0] != b[1]
is ever true for sub array a
. This is returning too many results, and you'll always end up returning the last index. For example, 'SEA', the 0 index of the 3rd sub array, does not equal 'BWI', the 1 index of the 0 sub array, so your start_point
now equals 3.
I won't do all of your work for you :), but let me suggest this: When you're going through your iterations, keep track of which sub arrays' index 0 ever equals a different sub array's index 1. Your answer will be the only one not included in this list.
Edit: Continue to work through my above suggestion for good practice, but here's a real quick and short solution:
def find_start_point(list)
list.each_with_index do |sub, idx|
return idx if list.flatten.count(sub[0]) == 1
end
end
This works by returning the index of the sub array with an index 0 where there are no other occurrences of that airport (by flattening the entire array and using #count
)