How do I add together two arrays and get the result back as one number? Ruby - ruby

I need to add together two arrays of numbers and print the result as a total number.
arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
arr1 + arr2
Just gives me = 1 2 3 4 5 6, but I want 21.

you can use the sum method
arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
(arr1 + arr2).sum

This is another way
arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
p (arr1 + arr2).reduce(:+)
Output
21

Related

How can I get the next n number of elements using a Ruby enumerator?

I am trying to get the next n number of elements using a Ruby enumerator, with this:
a = [1, 2, 3, 4, 5, 6]
enum = a.each
enum.next(2) # expecting [1, 2]
enum.next(2) # expecting [3, 4]
But #next does not support that. Is there another way that I can do that?
Or shall I do?
What is the correct Ruby way to do that?
You can use take method
enum.take(2)
If you need slices of two elements, you could do:
e = enum.each_slice(2)
p e.next
#=> [1, 2]
p e.next
#=> [3, 4]
a = [1, 2, 3, 4, 5, 6]
enum = a.dup
enum.shift(2) # => [1, 2]
enum.shift(2) # => [3, 4]

Pop matching elements from array, then push them back

Say I have an array: [1, 2, 3, 4, 5].
Given another array ([2, 4], for example), I would like to have a new array (or the initial array modified, doesn't matter) that looks like: [1, 3, 5, 2, 4]. So selected elements are moved to the end of the array.
Pushing the elements back is quite straight-forward, but how do I pop specific elements from an array?
a = [1, 2, 3, 4, 5]
b = [2, 4]
(a - b) + (b & a)
#=> [1, 3, 5, 2, 4]
a - b is the elements in a but not in b, while b & a is the elements that are common in both arrays. There goes your expected result.
In case if elements in a are not uniq (as mentioned by eugen) and it's important to remove only one element from b you could do something like:
a = [1, 2, 3, 2, 4, 5, 4, 2]
b = [2, 4, 7]
p (b&a).unshift(a.map{|el|
b.include?(el) ? begin b = b -[el]; nil end : el
}.compact).flatten
#=> [1, 3, 2, 5, 4, 2, 2, 4]

Get the exact differences of two arrays

I have two arrays:
array1 = [1,2,2,4,5,6]
array2 = [2,1]
How do I get
array3 = [2,4,5,6]
I have tried array1 - array2, but it returns [4,5,6].
What you are describing is a multiset. There is no implementation in the standard library, but you can use the multiset gem.
require 'multiset'
ms1 = Multiset.new([1, 2, 2, 4, 5, 6])
ms2 = Multiset.new([2, 1])
ms1 - ms2
#=> #<Multiset:#1 2, #1 4, #1 5, #1 6>
(ms1 - ms2).to_a
#=> [2, 4, 5, 6]
You could find each element's index and delete that one, as shown in this answer:
array1 = [1,2,2,4,5,6]
array2 = [2,1]
array2.each { |obj| array1.delete_at(array1.index(obj) || array1.length) }
array1 #=> [2, 4, 5, 6]

How to multiply every other element of an array?

Let's say that I have an array like this:
[1,2,3,4,5,6,7]
how can I multiply every other number of this array except the first by 2
so my new array looks like this
[1,4,3,8,5,12,7]
You can use map and with_index:
[1,2,3,4,5,6,7].map.with_index{|v,i| i % 2 == 0 ? v : v * 2 }
# => [1, 4, 3, 8, 5, 12, 7]
[1,2,3,4,5,6,7].each_slice(2).flat_map{|k, l| [k, *(l * 2 if l)]}
# => [1, 4, 3, 8, 5, 12, 7]

Replace from one array values of another array

I have two arrays:
array1 = [3, 4, 4, 5, 6, 7, 8, 8]
array2 = [4, 5, 8, 8]
I want to remove those elements of array1, which are found in array2, but only in one instance. The resulting array, array3, must be like this:
array3 = [3, 4, 6, 7]
I tried:
array3 = array1 - array2
but the result was unsatisfactory:
array3 -> [3, 6, 7]
This may not be the most efficient way of doing what you want, but it works:
array1 = [3, 4, 4, 5, 6, 7, 8, 8]
array2 = [4, 5, 8, 8]
array2.each do |item|
index = array1.index item
array1.delete_at index if index
end
A non-imperative just to show other ways of doing things. Using Facets (just for convenience to get the histogram), I'd write this. O(n):
require 'facets'
array3 = array1.reduce([array2.frequency, []]) do |(h, output), x|
if h[x] && h[x] > 0
[h.update(x => h[x] - 1), output]
else
[h, output << x]
end
end[1]
#=> [3, 4, 6, 7]
To make the snippet purely functional you would use Hash#merge/Array#+ instead of Hash#update/Array:<<, but due to the nature of these data structures it would be terribly inefficient.

Resources