Is it possible to get the difference from two dynamic arrays regardless of their order?
2014-06-14 16:27
525 查看
If I have two arrays and I try to find their difference..
----------------------------------------------------------------------------------------------------------
require 'set'
([2, 1].to_set ^ [1, 2, 3, 2, 6, 7]).to_a
# => [3, 6, 7]
([1, 2, 3, 2, 6, 7].to_set ^ [2, 1]).to_a
# => [3, 6, 7]
According to the documentation
----------------------------------------------------------------------------------------------------------------------------------------------------------------
You can define it:
class Array
def diff(o)
(o - self) + (self - o) # alternatively: (o + self) - (o & self)
end
end
[2, 1].diff [1, 2, 3, 2, 6, 7] # [3, 6, 7]
[1, 2, 3, 2, 6, 7].diff [2, 1] # [3, 6, 7]
[2, 3, 3, 1].diff [2, 4, 5] # [4, 5, 3, 3, 1]
[2, 4, 5].diff [2, 3, 3, 1] # [3, 3, 1, 4, 5]
[1, 2, 3, 2, 6, 7] - [2, 1]I get :
[3, 6, 7]But if I flip those arrays around
[2, 1] - [1, 2, 3, 2, 6, 7]I get :
[]My question is, being that my two arrays are dynamic, I need to know if there is a difference in between both arrays regardless of their order. What's the simplest expression to find that?
----------------------------------------------------------------------------------------------------------
require 'set'
([2, 1].to_set ^ [1, 2, 3, 2, 6, 7]).to_a
# => [3, 6, 7]
([1, 2, 3, 2, 6, 7].to_set ^ [2, 1]).to_a
# => [3, 6, 7]
According to the documentation
Set#^returns a new set containing elements exclusive between the set and the given enumerable object.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
You can define it:
class Array
def diff(o)
(o - self) + (self - o) # alternatively: (o + self) - (o & self)
end
end
[2, 1].diff [1, 2, 3, 2, 6, 7] # [3, 6, 7]
[1, 2, 3, 2, 6, 7].diff [2, 1] # [3, 6, 7]
[2, 3, 3, 1].diff [2, 4, 5] # [4, 5, 3, 3, 1]
[2, 4, 5].diff [2, 3, 3, 1] # [3, 3, 1, 4, 5]
相关文章推荐
- It is not possible to run two different versions of ASP.NET in the same IIS process
- It is not possible to run two different versions of ASP.NET in the same IIS process:IIS
- It is not possible to run two different versions of ASP.NET in the same IIS process.问题的解决
- Resolution to the record count increasing of the file exported from DB when ‘0A’ is included in it
- 布置Git环境Your working copy is out of date. Try pulling from the remote to get the latest change
- Xcode4 布置Git环境Your working copy is out of date. Try pulling from the remote to get the latest change
- Resolution to the record count increasing of the file exported from DB when ‘0A’ is included in it
- Resolution to the record count increasing of the file exported from DB when ‘0A’ is included in it
- [Drupal] How to get the real path of a node, no matter it is a path or a url alias
- Xcode4 布置Git环境Your working copy is out of date. Try pulling from the remote to get the latest change
- Is it possible to use a checkbox in a split button list instead of the gear icon?
- Question 30: What is the order of destructor calls for an object of class Y inherited from class X that has an object of class A
- Using Timer to get history values from EDNA database, an error is prompted in the page : "Sys.WebForms.PageRequestManagerTimeout
- How to get a type in C++ when its template argument is the argument
- Unable to get value of the property '2': object is null or undefined
- How to get a notification from Linux when the set of network interfaces changes
- get paid from、 to the taste of、expand my horizons、available、by the way
- just of bright mans is two people of the eyebrows from this through
- 学语言最好的方法是尽量多地联系使用它。(The way to learn a language is to practice using it as often as possible.)
- Microsoft SQL Server 2008 R2 XML处理 One solution is to increase the number of characters retrieved from the server for XML data.