Let's say that I have a multidimensional array with array's inside of it that each have two numbers. How do I iterate over the entire array and output the lowest number in each array? For example [[4, 6][8, 3]].My attempts at using .min, <=>, and if else statements have not worked.
Assuming you want to list 'all the mins from the arrays', there are lots of ways, here is a simple one:
array_of_arrays = [[4,6],[8,3]]
lowest_arrays = array_of_arrays.map {|a| a[0] < a[1] ? a[0] : a[1]}
or
lowest_arrays = array_of_arrays.map {|a| a.min}
This outputs [4, 3]
Should be as simple as:
[[4, 6],[8, 3]].each{|a| puts a.min}
or
[[4, 6],[8, 3]].map{|a| a.min}
... for an array output
Just use flatten followed by min like so:
[[4, 6], [8, 3]].flatten.min
=> 3
Related
I would like to find the sum of each row of a multidimensional array, and have the sums an array, e.g., for [[1,2,3],[1,1,1]], I would like to get [6,3].
I tried the following:
arr = [[1,2,3],[3,2,1],[2,1,3]]
print arr.each{|row| row.each{|column| puts column}}
Results:
1
2
3
3
2
1
2
1
3
[[1, 2, 3], [3, 2, 1], [2, 1, 3]]
I am struggling with it. I still don't fully understand each iterators. Any help would be appreciated.
For ruby 2.4.0 or newer
a.map { |suba| suba.sum }
or simply
a.map(&:sum)
for ruby prior to 2.4.0
a.map { |suba| suba.inject(0, :+) }
[[1,2,3],[1,1,1]].map{|a| a.inject(:+)} # => [6, 3]
If there is a possibility that any of the sub-array can be empty, then you need to add the initial 0, as Ursus pointed out.
[[1,2,3],[1,1,1]].map{|a| a.inject(0, :+)} # => [6, 3]
[[1,2,3],[1,1,1]].map { |a| a.inject(0, :+) } # => [6 , 3]
map changes each element to the return value of this elements block
get the sum for each array with inject(:+)
use inject(0, :+) to set 0 instead of the first element as the start value (handles empty inner array)
see:
Enumerable#inject
Enumerable#map
"How to find the sum of each row"
arr = [[1,2,3], [1,1,1]]
print arr.each{|row| <-- here you have each row
So now row contains [1,2,3] initially. As others have mentioned, you can apply a sum here. (You don't need the leading print).
arr.each{|row| puts row.sum}
Result:
6
3
But a better way to do it is with map. As I told a Ruby newbie many years ago, think of map when you want to change every element to something else, in other words a "1:1 mapping" of input to output. In this case the output is row.sum:
sums = arr.map{|row| row.sum}
Result:
[6, 3]
I have an ordered array which contain 1 to 1000000 elements.
I want to achieve an array such that the elements in the array are swapped with its next element.For instance if we assume the array elements are
[1,2,3,4,5,6]
I want to return an array with elements as
[2,1,4,3,6,5]
How do I achieve this in ruby for 100000 such elements? Can anyone guide me?
a = [1,2,3,4,5,6]
a.each_slice(2).map{|inner_a| inner_a.reverse}.flatten
# => [2, 1, 4, 3, 6, 5]
Description:
a.each_slice(2)returns an enumerator (#<Enumerator: [1, 2, 3, 4, 5, 6]:each_slice(2)>) with two element couples from your array. To see try a.each_slice(2).to_a. This returns [[1, 2], [3, 4], [5, 6]] with I only have to flatten for your expected result.
See also the first comment if you prefer a shorter notation of it.
Assuming you want to use a minimum amount of memory (since you chose a large array), and assuming the result is to be a mutated array (i.e. not a new array, but a change to the existing array) and finally assuming a is always an even number of elements...
a.each_index{|x| a[x], a[x+1] = a[x+1], a[x] if x.even?}
Possibly more performant...
(0...a.size).step(2) {|x| a[x], a[x+1] = a[x+1], a[x]}
You can try this.
arr = (1..100000).to_a
arr.each_with_index.each_slice(2){|(_,i), (_,j)| arr[i], arr[j] = arr[j], arr[i]}
I am trying to print all the different sums of all combinations in this array [1,2,3]. I want to first push every sum result to a new array b, then print them using b.uniq so that non of the sum results are repeated.
However, with the code I have, the 3 repeats itself, and I think it is because of the way it is pushed into the array b.
Is there a better way of doing this?
a = [1,2,3]
b = []
b.push a
b.push a.combination(2).collect {|a,b| (a+b)}
b.push a.combination(3).collect {|a,b,c| (a+b+c)}
puts b.uniq
p b #[[1, 2, 3], [3, 4, 5], [6]]
Can someone please help me with this? I am still new in ruby.
Because an Array of arbitrary length can be summed using inject(:+), we can create a more general solution by iterating over the range 1..n, where n is the length of the Array.
(1..(a.size)).flat_map do |n|
a.combination(n).map { |c| c.inject(&:+) }
end.uniq
#=> [1, 2, 3, 4, 5, 6]
By using flat_map, we can avoid getting the nested Array result, and can call uniq directly on it. Another option to ensure uniqueness would be to pass the result to a Set, for which Ruby guarantees uniqueness internally.
require "set"
sums = (1..(a.size)).flat_map do |n|
a.combination(n).map { |c| c.inject(&:+) }
end
Set.new(sums)
#=> #<Set: {1, 2, 3, 4, 5, 6}>
This will work for an any Array, as long as all elements are Fixnum.
If all you want is an array of the possible sums, flatten the array before getting the unique values.
puts b.flatten.uniq
What is happening is uniq is running over a multi-dimensional array. This causes it to look for duplicate arrays in your array. You'll need the array to be flattened first.
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.
I have the following array:
[["2010-01-10", 2], ["2010-01-09", 5], ["2009-12-11", 3], ["2009-12-12", 12], ["2009-12-13", 0]]
I just want to sort it by the second value in each group and return the highest one, like i want to the output to be 12 with the given input above.
update
I might add that I made this into an array using to_a, from a hash, so if there is away to do the same with a hash that would be even better.
To sort by second value
x=[["2010-01-10", 2], ["2010-01-09", 5], ["2009-12-11", 3], ["2009-12-12", 12], ["2009-12-13", 0]]
x.sort_by{|k|k[1]}
=> [["2009-12-13", 0], ["2010-01-10", 2], ["2009-12-11", 3], ["2010-01-09", 5], ["2009-12-12", 12]]
Call the sort method on your hash to sort it.
hash = hash.sort { |a, b| b[1] <=> a[1] }
Then convert your hash to an array and extract the first value.
result = hash.to_a[0][1]
Use this on your hash:
hash.values.max
If you only need the highest element, there is no need to sort it!
If you want the key-value pair with the max value:
hash.max_by {|key, val| val} # => ["2009-12-12", 12]
requires Ruby 1.8.7+