array question

L

Li Chen

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
 
N

Nathan Powell

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.
=> [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]

--
nathan
nathan_at_nathanpowell_dot_org

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

Rob Biedenharn

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.
=> [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]

--
nathan
nathan_at_nathanpowell_dot_org

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

irb> (a1 | a2) - (a1 & a2)
=> [2, 3, 4, 7]

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
J

Joel VanderWerf

Nathan said:
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]

Or:

(a1|a2) - (a1&a2)

Or:

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


(I thought I remem
 
S

Siep Korteling

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
 
S

Siep Korteling

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
 
A

Amos King

Add the two arrays and call uniq on the new array

Joel VanderWerf wrote:
(...)
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
 
N

Nathan Powell

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
------------------------------------
 
A

Amos King

We'll see.

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
 
A

Amos King

Forgot to ask what he was looking for.

We'll see.

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
 
N

Nathan Powell

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
------------------------------------
 
T

Tod Beardsley

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)

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
 
A

Amos King

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

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)

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



--
(e-mail address removed) | ICQ: 335082155 | Note: Due to Google's
privacy policy <http://tinyurl.com/5xbtl> and the United States'
policy on electronic surveillance <http://tinyurl.com/muuyl>,
please do not IM/e-mail me anything you wish to remain secret.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top