Array question

Hi all,

Just wonder:

1)When substracting array2 from array1 to get the difference elements
between two arrays(array1-array2), must array1.size >= array2?

2)Is there mentod in Array class that can return the element only
unique to either of two arrays?

For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]

Thanks,

Li

···

--
Posted via http://www.ruby-forum.com/.

Hi all,

Just wonder:

1)When substracting array2 from array1 to get the difference elements
between two arrays(array1-array2), must array1.size >= array2?

irb is great for this stuff.

[1, 2] - [1, 3, 4]

=> [2]

2)Is there mentod in Array class that can return the element only
unique to either of two arrays?

http://ruby-doc.org/core/classes/Array.html

For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]

There might be a better way, but I came up with:

([1, 2, 3] - [1, 4, 7]) + ([1, 4, 7] - [1, 2, 3])

=> [2, 3, 4, 7]

···

On Tue, Sep 16, 2008 at 05:27:03AM +0900, Li Chen wrote:

--
nathan
nathan_at_nathanpowell_dot_org

What kind of crazy nut would spend two or three hours a day just running?
     ~ Steve Prefontaine
------------------------------------

Li Chen wrote:
(...)

2)Is there mentod in Array class that can return the element only
unique to either of two arrays?

For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]

Thanks,

Li

It's probably academic, but what is the desired result with these 2
arrays?
array1 = [1,2,2,3]
array2 = [1,4,7,7]

Nathan Powell's method: [2, 2, 3, 4, 7, 7]
Rob Biedenharn's method: [2, 3, 4, 7]
Perhaps desired result: [3,4] ?

Regards,

Siep

···

--
Posted via http://www.ruby-forum.com/\.

=> [2, 3, 4, 7]

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Sep 15, 2008, at 4:42 PM, Nathan Powell wrote:

On Tue, Sep 16, 2008 at 05:27:03AM +0900, Li Chen wrote:

Hi all,

Just wonder:

1)When substracting array2 from array1 to get the difference elements
between two arrays(array1-array2), must array1.size >= array2?

irb is great for this stuff.

[1, 2] - [1, 3, 4]

=> [2]

2)Is there mentod in Array class that can return the element only
unique to either of two arrays?

class Array - RDoc Documentation

For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]

There might be a better way, but I came up with:

([1, 2, 3] - [1, 4, 7]) + ([1, 4, 7] - [1, 2, 3])

=> [2, 3, 4, 7]

-- nathan
nathan_at_nathanpowell_dot_org

What kind of crazy nut would spend two or three hours a day just running?
     ~ Steve Prefontaine
------------------------------------

(a1 | a2) - (a1 & a2)

Nathan Powell wrote:
...

2)Is there mentod in Array class that can return the element only
unique to either of two arrays?

class Array - RDoc Documentation

For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]

There might be a better way, but I came up with:

([1, 2, 3] - [1, 4, 7]) + ([1, 4, 7] - [1, 2, 3])

=> [2, 3, 4, 7]

Or:

   (a1|a2) - (a1&a2)

Or:

  require 'set'
  Set.new(a1) ^ Set.new(a2)

(I thought I remem

···

On Tue, Sep 16, 2008 at 05:27:03AM +0900, Li Chen wrote:

Joel VanderWerf wrote:

require 'set'
Set.new(a1) ^ Set.new(a2)

(I thought I remem

                     bered that Array had a symmetric difference operator, but apparently not.)

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel VanderWerf wrote:
(...)

Or:

  require 'set'
  Set.new(a1) ^ Set.new(a2)

I figured this out by typo, allthough it is in the documentation.
Anyway, you can do
Set.new(a1) ^ a2

if a2 is enumerable.

Regards,

Siep

···

--
Posted via http://www.ruby-forum.com/\.

Add the two arrays and call uniq on the new array

···

On Mon, Sep 15, 2008 at 4:39 PM, Siep Korteling <s.korteling@gmail.com> wrote:

Joel VanderWerf wrote:
(...)

Or:

  require 'set'
  Set.new(a1) ^ Set.new(a2)

I figured this out by typo, allthough it is in the documentation.
Anyway, you can do
Set.new(a1) ^ a2

if a2 is enumerable.

Regards,

Siep
--
Posted via http://www.ruby-forum.com/\.

--
Amos King
A. King Software Development and Consulting, L.C.
http://dirtyInformation.com
--
Looking for something to do? Visit http://ImThere.com

=> [1, 2, 3, 4, 7]

That doesn't result in what he asked it to result in. I am not
convinced what I came up with was what he was really after either. I
think Siep was on to something.

···

On Tue, Sep 16, 2008 at 11:12:50PM +0900, Amos King wrote:

Add the two arrays and call uniq on the new array

([1, 2, 3] + [1, 4, 7]).uniq

--
nathan
nathan_at_nathanpowell_dot_org

What kind of crazy nut would spend two or three hours a day just running?
     ~ Steve Prefontaine
------------------------------------

We'll see.

···

On Tue, Sep 16, 2008 at 9:18 AM, Nathan Powell <nathan@nathanpowell.org> wrote:

On Tue, Sep 16, 2008 at 11:12:50PM +0900, Amos King wrote:

Add the two arrays and call uniq on the new array

([1, 2, 3] + [1, 4, 7]).uniq

=> [1, 2, 3, 4, 7]

That doesn't result in what he asked it to result in. I am not
convinced what I came up with was what he was really after either. I
think Siep was on to something.

--
nathan
nathan_at_nathanpowell_dot_org

What kind of crazy nut would spend two or three hours a day just running?
                ~ Steve Prefontaine
------------------------------------

--
Amos King
A. King Software Development and Consulting, L.C.
http://dirtyInformation.com
--
Looking for something to do? Visit http://ImThere.com

Forgot to ask what he was looking for.

···

On Tue, Sep 16, 2008 at 9:29 AM, Amos King <amos.l.king@gmail.com> wrote:

We'll see.

On Tue, Sep 16, 2008 at 9:18 AM, Nathan Powell <nathan@nathanpowell.org> wrote:

On Tue, Sep 16, 2008 at 11:12:50PM +0900, Amos King wrote:

Add the two arrays and call uniq on the new array

([1, 2, 3] + [1, 4, 7]).uniq

=> [1, 2, 3, 4, 7]

That doesn't result in what he asked it to result in. I am not
convinced what I came up with was what he was really after either. I
think Siep was on to something.

--
nathan
nathan_at_nathanpowell_dot_org

What kind of crazy nut would spend two or three hours a day just running?
                ~ Steve Prefontaine
------------------------------------

--
Amos King
A. King Software Development and Consulting, L.C.
http://dirtyInformation.com
--
Looking for something to do? Visit http://ImThere.com

--
Amos King
A. King Software Development and Consulting, L.C.
http://dirtyInformation.com
--
Looking for something to do? Visit http://ImThere.com

In the original email:

<snip>
For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]
</snip>

It's not entirely clear as one could arrive at that final array a couple
ways.

···

On Tue, Sep 16, 2008 at 11:25:00PM +0900, Amos King wrote:

Forgot to ask what he was looking for.

--
nathan
nathan_at_nathanpowell_dot_org

At this point, anyone proposing to run Windows on servers should be prepared
to explain what they know about servers that Google, Yahoo, and Amazon don't.
     ~ Paul Graham
------------------------------------

Case 1 and 2, maybe the desired result?

irb(main):088:0> def there_can_be_only_one(arr)
irb(main):089:1> arr.sort!.collect {|x| (arr[arr.index(x)] !=
arr[arr.index(x)+1] ? x:nil)}.compact
irb(main):090:1> end
=> nil
irb(main):091:0> there_can_be_only_one([1,2,3] + [1,4,7])
=> [2, 3, 4, 7]
irb(main):092:0> there_can_be_only_one([1,2,2,3] + [1,4,7,7])
=> [3, 4]
irb(main):093:0>

...assumes the elements to be compared are all the same type (Fixnums
can't sort with Strings, eg)

···

On Tue, Sep 16, 2008 at 9:29 AM, Nathan Powell <nathan@nathanpowell.org> wrote:

On Tue, Sep 16, 2008 at 11:25:00PM +0900, Amos King wrote:

Forgot to ask what he was looking for.

In the original email:

<snip>
For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]
</snip>

It's not entirely clear as one could arrive at that final array a couple
ways.

--
nathan
nathan_at_nathanpowell_dot_org

At this point, anyone proposing to run Windows on servers should be prepared
to explain what they know about servers that Google, Yahoo, and Amazon don't.
                ~ Paul Graham
------------------------------------

--
todb@planb-security.net | ICQ: 335082155 | Note: Due to Google's
privacy policy <http://tinyurl.com/5xbtl&gt; and the United States'
policy on electronic surveillance <http://tinyurl.com/muuyl&gt;,
please do not IM/e-mail me anything you wish to remain secret.

That only works if they are touching and that can be accomplished with
inject really easy.

([1,2,2,3]+[1,4,7,7]).sort..inject() { |final, e| fianl[-1] == e?
final[0..-2] : final << e }
#=> [3,4]

([1,2,3]+[1,4,7]).sort..inject() { |final, e| fianl[-1] == e?
final[0..-2] : final << e }
#=> [2,3,4,7]

although with the later one you can just do
([1,2,3]+[1,4,7]).uniq

···

On Tue, Sep 16, 2008 at 10:08 AM, Tod Beardsley <todb@planb-security.net> wrote:

Case 1 and 2, maybe the desired result?

irb(main):088:0> def there_can_be_only_one(arr)
irb(main):089:1> arr.sort!.collect {|x| (arr[arr.index(x)] !=
arr[arr.index(x)+1] ? x:nil)}.compact
irb(main):090:1> end
=> nil
irb(main):091:0> there_can_be_only_one([1,2,3] + [1,4,7])
=> [2, 3, 4, 7]
irb(main):092:0> there_can_be_only_one([1,2,2,3] + [1,4,7,7])
=> [3, 4]
irb(main):093:0>

...assumes the elements to be compared are all the same type (Fixnums
can't sort with Strings, eg)

On Tue, Sep 16, 2008 at 9:29 AM, Nathan Powell <nathan@nathanpowell.org> wrote:

On Tue, Sep 16, 2008 at 11:25:00PM +0900, Amos King wrote:

Forgot to ask what he was looking for.

In the original email:

<snip>
For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]
</snip>

It's not entirely clear as one could arrive at that final array a couple
ways.

--
nathan
nathan_at_nathanpowell_dot_org

At this point, anyone proposing to run Windows on servers should be prepared
to explain what they know about servers that Google, Yahoo, and Amazon don't.
                ~ Paul Graham
------------------------------------

--
todb@planb-security.net | ICQ: 335082155 | Note: Due to Google's
privacy policy <http://tinyurl.com/5xbtl&gt; and the United States'
policy on electronic surveillance <http://tinyurl.com/muuyl&gt;,
please do not IM/e-mail me anything you wish to remain secret.

--
Amos King
A. King Software Development and Consulting, L.C.
http://dirtyInformation.com
--
Looking for something to do? Visit http://ImThere.com