3

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.

Community
  • 1
  • 1

4 Answers4

5

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]
dbenhur
  • 20,008
  • 4
  • 48
  • 45
3

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(:+)}
cjc343
  • 3,735
  • 1
  • 29
  • 34
3

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
Jakub Hampl
  • 39,863
  • 10
  • 77
  • 106
1

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']
user229044
  • 232,980
  • 40
  • 330
  • 338