发现在一个化合物阵列的唯一元件 [英] Find a unique element in a compound array

查看:167
本文介绍了发现在一个化合物阵列的唯一元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决一个问题,我需要找到机场code。在重新$ P $的数组的数组psents一个多城市飞行计划的起点。
例如:给定一个数组[['洛杉矶','BWI'],['BOS','海'],['HNL','洛杉矶'],['海','HNL'],其中第一每个子阵列的索引是
出发机场第二是目的地机场,我需要找到索引地步飞行起源,在这种情况下,方法将返回1重新present ['BOS','海'。

这code不能正常工作并始终返回的最后一个元素数组中的

 高清find_start_point(名单)
  start_point =零
  list.each做| A |
    list.each做| B |
      如果一个[0]!= B [1]
        start_point = list.index(一)
      结束
    结束
  结束
  start_point
结束


解决方案

我看你尝试的东西。始发机场将是一个0指数的子阵列而不是1指数的子阵列包括唯一的一个。

不过,在你的解决方案,你是比较2子阵列的每个组合(包括子阵列本身,顺便......),然后返回列表的指数如果 A [0]!= b [1] 是有史以来子阵列真正的 A 。这是返回过多的结果,而你将永远结束​​返回最后一个索引。例如,'海',第三子阵列的0指数,不等于BWI,0分数组的1指数,所以你的 start_point 现在等于3。

我不会做你为你:)的工作,但我认为这样的:当你要通过你的迭代,跟踪哪些子阵列人指数有史以来0等于不同的子数组索引1。你的回答将不包括在此名单唯一的一个。

编辑:继续通过我的良好做法以上建议工作,但这里有一个真正的快速和短期解决办法:

 高清find_start_point(名单)
  list.each_with_index做|子,IDX |
    返回IDX如果list.flatten.count(子[0])== 1
  结束
结束

这工作与指数0,其中有一些机场没有其他事件返回子数组的索引(由压扁整个阵列,并使用 #COUNT

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 an array [['LAX', 'BWI'], ['BOS', 'SEA'], ['HNL', 'LAX'], ['SEA', 'HNL']] where the first index of each sub array is the departing airport and the second is the destination airport, I need to find the index point where the flight originates, in this case the method would return 1 to represent ['BOS', 'SEA'].

This code does not work and always returns the last element in the array

def find_start_point(list)
  start_point = nil
  list.each do |a|
    list.each do |b|
      if a[0] != b[1]
        start_point = list.index(a)
      end
    end
  end
  start_point
end

解决方案

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)

这篇关于发现在一个化合物阵列的唯一元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆