Compare two arrays in Ruby - ruby

I'd like to know whether two Ruby arrays have the same elements, though not necessarily in the same order. Is there a native way to do this? The equality operators for Array seem to check whether the items are the same and the order is the same, and I need to relax the latter condition.
This would be extremely easy to write, I just wonder if there's a native idiom.

If you don't have duplicate items either, you could use Set instead of Array:
Set implements a collection of unordered values with no duplicates.
This is a hybrid of Array's intuitive inter-operation facilities and
Hash's fast lookup.
Example:
require 'set'
s1 = Set.new [1, 2, 3] # -> #<Set: {1, 2, 3}>
s2 = [3, 2, 1].to_set # -> #<Set: {3, 2, 1}>
s1 == s2 # -> true

[2,1].uniq.sort == [1,2].uniq.sort #=> true
[2,1,4].uniq.sort == [1,2].uniq.sort #=> false
or
a1 = [1,2,3]
a2 = [2,3,1]
p (a2-a1).empty? && (a1-a2).empty? #=> true
a1 = [1,2,3]
a2 = [4,3,1]
p (a2-a1).empty? && (a1-a2).empty? #=> false
a1 = [1,2,3]
a2 = [2,3,1,5]
p (a2-a1).empty? && (a1-a2).empty? #=> false

This would be extremely easy to write, I just wonder if there's a native idiom.
I'm afraid there's no native idiom for it.
If your arrays contains multiple values that you want to count on both arrays you'll have to use #sort to put them in the same order. Once you have done that you can easily compare them:
a.sort == b.sort
Otherwise you can use #uniq that will extract the unique values of the arrays (to make it faster) and use #sort like above:
a.uniq.sort == b.uniq.sort

a1 = [1, 2, 3, 4]
a2 = [4, 2, 1, 3]
(a1 & a2).size == a1.size # => true
a3 = [1, 2, 3, 5]
(a1 & a3).size == a1.size # => false

Related

What does a comma followed by an equals sign mean in Ruby?

Just saw something like this in some Ruby code:
def getis;gets.split.map(&:to_i);end
k,=getis # What is this line doing?
di=Array::new(k){Array::new(k)}
It assigns the array's first element using Ruby's multiple assignment:
a, = [1, 2, 3]
a #=> 1
Or:
a, b = [1, 2, 3]
a #=> 1
b #=> 2
You can use * to fetch the remaining elements:
a, *b = [1, 2, 3]
a #=> 1
b #=> [2, 3]
Or:
*a, b = [1, 2, 3]
a #=> [1, 2]
b #=> 3
It works like this. If lhs has single element and rhs has multiple values then lhs gets assigned an array of values, like this.
a = 1,2,3 #=> a = [1,2,3]
Whereas if lhs has more elements than rhs, then excess elements in lhs are discarded
a,b,c = 1,2 #=> a = 1, b = 2, c = nil
Therefore
a, = 1,2,3 #=> a = 1. The rest i.e. [2,3] are discarded

Adding two arrays in Ruby when the array length will always be the same

So, I need to add two arrays together to populate a third. EG
a = [1,2,3,4]
b = [3,4,5,6]
so that:
c = [4,6,8,10]
I read the answer given here: https://stackoverflow.com/questions/12584585/adding-two-ruby-arrays
but I'm using the codecademy labs ruby editor and it's not working there, plus the lengths of my arrays are ALWAYS going to be equal. Also, I don't have any idea what the method ".with_index" is or does and I don't understand why it's necessary to use ".to_i" when the value is already an integer.
It seems like this should be really simple?
a = [1,2,3,4]
b = [3,4,5,6]
a.zip(b).map { |i,j| i+j } # => [4, 6, 8, 10]
Here
a.zip(b) # => [[1, 3], [2, 4], [3, 5], [4, 6]]
and map converts each 2-tuple to the sum of its elements.
OPTION 1:
For a pure Ruby solution, try the transpose method:
a = [1,2,3,4]
b = [3,4,5,6]
c = [a, b].transpose.map{|x, y| x + y}
#=> [4,6,8,10]
OPTION 2:
If you're in a Rails environment, you can utilize Rails' sum method:
[a, b].transpose.map{|x| x.sum}
#=> [4,6,8,10]
EXPLANATION:
transpose works perfectly for your scenario, since it raises an IndexError if the sub-arrays don't have the same length. From the docs:
Assumes that self is an array of arrays and transposes the rows and columns.
If the length of the subarrays don’t match, an IndexError is raised.

Add two arrays into another array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ruby: sum corresponding members of two arrays
So I'm struggling to do this.
I am trying to add two arrays and put the results into a third one. Now I want to be able to do this through a function though.
So I have Array_One, Array_Two, and Array_Three.
I would want to call the "compare function" to one and two and make Three that length and then if they the lengths matched I would want to add One and Two and put the results in three.
Could this be done in one function?
Better way to do this?
That's my thought process but I don't have the knowledge of Ruby to do this.
EDIT:
Sorry for the vagueness.
Array_One = [3,4]
Array_Two = [1,3]
Array_Three= []
I would want to pass One and Two through a function that compares the length and verifies they're the same length.
Then I would, in my mind, send it through a function that actually does the adding.
So in the end I would have Array_Three = [4,7]
Hope that helps.
Your question's description is little confusing, but I think this may be what you want:
def add_array(a,b)
a.zip(b).map{|pair| pair.reduce(&:+) }
end
irb> add_array([1,2,3],[4,5,6])
=> [5, 7, 9]
In addition it generalize to add multiple arrays quite easily:
def add_arrays(first_ary, *other_arys)
first_ary.zip(*other_arys).map{|column| column.reduce(&:+) }
end
irb> add_arrays([1,2,3],[4,5,6])
=> [5, 7, 9]
irb> add_arrays([1,2,3],[4,5,6],[7,8,9])
=> [12, 15, 18]
One possible method (without any checks) assuming I understood your question:
x = [1, 2, 3]
y = [4, 5, 6]
z = []
x.each_with_index do |v, i|
z << v + y[i]
end
Another method (still assuming I understood the question):
[x, y].transpose.map {|v| v.reduce(:+)}
If by add you mean element-wise addition than you can use:
def add(a, b)
a.zip(b).map &:sum
end
assuming your environment has sum defined.
Sum is an alias for reduce(&:+), which you can use instead.
zip is a function that takes two arrays and returns an array of arrays:
[a1, a2, a3, ..., an].zip [b1, b2, b3, ..., bm]
#=> [[a1, b1], [a2, b2], [a3, b3], ..., [an, bn]]
# assuming n <= m
We than take our array of arrays and sum all numbers in that array together and than collect the results with map.
map is a function that takes a block and produces an array:
[c1, c2, c3, ..., cn].map &block
# => [block.call(c1), block.call(c2), block.call(c3), ..., block.call(cn)]
So if we get example input say
a = [1, 2, 3]
b = [4, 2, 5]
a.zip(b) #=> [[1,4], [2,2], [3,5]]
a.zip(b).map(&:sum) #=> [[1,4].sum, [2,2].sum, [3,5].sum] #=> [5, 4, 8]
Now we can check that the same length by using an if condition:
def add(a, b)
a.zip(b).map &:sum if a.size == b.size
end
If you want to add two arrays, you can simply add them:
array1 = [1, 2, 3]
array2 = ['a', 'b', 'c']
if array1.length == array2.length
array3 = array1 + array2
end
# array3 = [1, 2, 3, 'a', 'b', 'c']

Find values in common between two arrays

If I want to compare two arrays and create an interpolated output string if an array variable from array y exists in x how can I get an output for each matching element?
This is what I was trying but not quite getting the result.
x = [1, 2, 4]
y = [5, 2, 4]
x.each do |num|
puts " The number #{num} is in the array" if x.include?(y.each)
end #=> [1, 2, 4]
You can use the set intersection method & for that:
x = [1, 2, 4]
y = [5, 2, 4]
x & y # => [2, 4]
x = [1, 2, 4]
y = [5, 2, 4]
intersection = (x & y)
num = intersection.length
puts "There are #{num} numbers common in both arrays. Numbers are #{intersection}"
Will output:
There are 2 numbers common in both arrays. Numbers are [2, 4]
OK, so the & operator appears to be the only thing you need to do to get this answer.
But before I knew that I wrote a quick monkey patch to the array class to do this:
class Array
def self.shared(a1, a2)
utf = a1 - a2 #utf stands for 'unique to first', i.e. unique to a1 set (not in a2)
a1 - utf
end
end
The & operator is the correct answer here though. More elegant.

Comparing the order of three arrays with the same elements

I have three arrays. The arrays all have the same size and contain the same elements. However, the three arrays must not be in the same order. How do I verify that the elements are not in the same order?
Here's how I've implemented it:
all_elements_equal = true
array1.zip(array2, array3) do |first, second, third|
if first != second && second != third && first != third
all_elements_equal = false
end
end
If all_elements_equal is false, presumably the arrays are not in the same order. However, there is a possibility that this will not be the case if only one of the arrays is different and the other two are identical.
So, my question is, how do I make the test stronger, and is there a more elegant way of implementing the code? Full disclosure: I am new to Ruby.
Have you tried this?
array1 == array2 || array1 == array3 || array2 == array3
In general, if you have array arr of N such arrays, you can just check if there are any duplicates there:
arr.length == arr.uniq.length
because, for example:
[[1,2,3],[2,3,1],[1,2,3]].uniq
#=> [[1, 2, 3], [2, 3, 1]]
[[1,2,3],[2,3,1],[2,1,3]].uniq
#=> [[1, 2, 3], [2, 3, 1], [2, 1, 3]]
I don't know Ruby, but I think you need to inverse your logic.
anyElementEqual=false
do
if first==second || first==third || second==third
anyElementEqual=true
end
etc.

Resources