How to change element in array by its coordinate - ruby-on-rails

If I have coordinates of element in array (|row_index, column_index|) how can I change the value of element?
This is my array: element[1,1] == 1, and I need to change the value of element near it [1,2] to "1"
#data = [
[0,0,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
]
That's how I'm finding element == 1
#data.each_with_index do |row, row_index|
row.each_with_index do |value, column_index|
if value == 1
#data[row_index+1][column_index] = 1
end
end
end

Right now your code changes the value under the cell, that contains 1, but you've stated that you need to change it on the right. I believe your code should be the following:
#data.each_with_index do |row, row_index|
row.each_with_index do |value, column_index|
if value == 1
#data[row_index][column_index + 1] = 1
end
end
end
You should note, however, since you're changing your array while iterating over it, you'll continue to write 1 to the right, until the elements are done (that will cause an error you get):
#data = [
[0,0,0,0,0],
[0,1,1,1,1], # <-- each iteration will write to the next cell
[0,0,0,0,0],
[0,0,0,0,0]
]
If it's a desired behavior, just check your indexes stay in the array borders:
#data.each_with_index do |row, row_index|
row.each_with_index do |value, column_index|
if value == 1 && column_index < row.size - 1
#data[row_index][column_index + 1] = 1
end
end
end
Otherwise, just break from 2 loops at once:
all_done = false
#data.each_with_index do |row, row_index|
row.each_with_index do |value, column_index|
if value == 1 && column_index < row.size - 1
#data[row_index][column_index + 1] = 1
all_done = true
break
end
end
break if all_done
end

You just need to assign the value directly by coordinates.
irb(main):114:0> a[0][0] = 100
irb(main):115:0> a
=> [[100, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
irb(main):117:0> a[1][2] = 1
irb(main):118:0> a
=> [[100, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]
Be careful: element[1,1] is different with element[1][1]
element[start,length]: returns a subarray starting at the start index and continuing for length elements
element[row][column]: a element of two-dimensional array

One way of doing this will be to do something like below:
#data = [
[0,0,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
]
row = #data.find_index{ |x| x.include? 1 }
col = #data[row].find_index { |x| x == 1 }
#data[row][col.next] = 1

Related

How to get the next and previous element in an array, Ruby

I have this array and need to replace its elements by the multiplication of its previous and next.
I do the following:
array.each_with_index{|element, index|
next_element = array[index+1]
previous_element = array[index-1]
}
array.map! {|a|
if a == array.first
a = a * next_element
elsif a == array.last
a = a * previous_element
else
a = next_element * previous_element
end
}
I expect the following result:
array = [4, 1, 6, 7, 9, 3, 0] #given array
array = [4, 24, 7, 54, 21, 0, 0] #array replaced
I get the following error:
undefined local variable or method `next_element' for Arrays:Class
Is there a easy way of getting the previous and next element of a given array element?
Am I using the array.map! method right?
This would work:
array = [4, 1, 6, 7, 9, 3, 0]
[nil, *array, nil].each_cons(3).map { |l, m, r| (l || m) * (r || m) }
#=> [4, 24, 7, 54, 21, 0, 0]
The array is surrounded by nil values, so each element has neighbors. each_cons(3) then yiels each element along with its neighbors to map which multiplies the left (l) with the right (r) neighbor, falling back to the middle element (m) if one of the neighbors happens to be nil.
You can do as following,
[array[0..1].inject(:*)] + array[0..-2].map.with_index { |x,i| x * (array[i+2] || array[i+1]) }
# => [4, 24, 7, 54, 21, 0, 0]
You defined next_element and previous_element inside the loop, so they become undefined at the end. This is a simple work around of your code, I'll assume that
you want to keep first and last element unchanged. You dont need to use map
array.each_with_index do |element, index|
if element != array.first && element != array.last
array[index] = array[index+1] * array[index-1]
end
end
array => [4, 24, 168, 1512, 4536, 0, 0]
This is not what you expected, why? Because your element, array[index] will change after each iteration you can print array after each iteration to see the result
I would suggest you to use another array to save the value of array, like this
b =[]
array.each_with_index do |element, index|
b[index] = array[index]
if element != array.first && element != array.last
b[index] = array[index+1] * array[index-1]
end
end
Seems like each_cons would be a good fit here:
[array.first] + array.each_cons(3).map { |p, _, n| p * n } + [array.last]
#=> [4, 24, 7, 54, 21, 0, 0]
This needs some more work (e.g. if the array is empty this will return [nil, nil]) but I'm sure you can figure out these edge cases.

Ruby sum from nested hash

How can I return the total scores, strokes and rounds from the following array?
players = [{"Angel Cabrera"=>{"score"=>2, "strokes"=>146, "rounds"=>3}},
{"Jason Day"=>{"score"=>1, "strokes"=>145, "rounds"=>3}},
{"Bryson DeChambeau"=>{"score"=>0, "strokes"=>144, "rounds"=>3}},
{"Sergio Garcia"=>{"score"=>0, "strokes"=>144, "rounds"=>3}},
{"Ian Poulter"=>{"score"=>5, "strokes"=>162, "rounds"=>3}},
{"Vijay Singh"=>nil},
{"Jordan Spieth"=>{"score"=>-4, "strokes"=>140, "rounds"=>3}}]
I can get the strokes by doing the following but I know that isn't the best way to do it.
players.each do |x|
x.values()[0]["strokes"]
end
How can I return the sum of the strokes given the array above?
Here are three ways of doing that.
Use the form of Hash#update that employs a block to determine the values of keys that are present in both hashes being merged
players.map { |g| g.first.last }.
compact.
each_with_object({}) { |g,h| h.update(g) { |_,o,v| o+v } }
#=> {"score"=>4, "strokes"=>881, "rounds"=>18}
The steps:
a = players.map { |g| g.first.last }
#=> [{"score"=> 2, "strokes"=>146, "rounds"=>3},
# {"score"=> 1, "strokes"=>145, "rounds"=>3},
# {"score"=> 0, "strokes"=>144, "rounds"=>3},
# {"score"=> 0, "strokes"=>144, "rounds"=>3},
# {"score"=> 5, "strokes"=>162, "rounds"=>3},
# nil,
# {"score"=>-4, "strokes"=>140, "rounds"=>3}]
b = a.compact
#=> [{"score"=> 2, "strokes"=>146, "rounds"=>3},
# {"score"=> 1, "strokes"=>145, "rounds"=>3},
# {"score"=> 0, "strokes"=>144, "rounds"=>3},
# {"score"=> 0, "strokes"=>144, "rounds"=>3},
# {"score"=> 5, "strokes"=>162, "rounds"=>3},
# {"score"=>-4, "strokes"=>140, "rounds"=>3}]
b.each_with_object({}) { |g,h| h.update(g) { |_,o,v| o+v } }
#=> {"score"=>4, "strokes"=>881, "rounds"=>18}
Here, Hash#update (aka merge!) uses the block { |_,o,v| o+v }) to determine the values of keys that are present in both hashes. The first block variable (which is not used, and therefore can be represented by the local variable _) is the key, the second (o, for "old") is the value of the key in h and the third (n, for "new") is the value of the key in g.
Use a counting hash
players.map { |g| g.first.last }.
compact.
each_with_object(Hash.new(0)) { |g,h| g.keys.each { |k| h[k] += g[k] } }
Hash.new(0) creates an empty hash with a default value of zero, represented by the block variable g. This means that if a hash h does not have a key k, h[k] returns the default value (but does not alter the hash). h[k] += g[k] above expands to:
h[k] = h[k] + g[k]
If h does not have a key k, h[k] on the right side is therefore replaced by 0.
Sum values and then convert to a hash
If you are using Ruby v1.9+ and the keys are guaranteed to have the same order in each hash, a third way it could be done is as follows:
["scores", "strokes", "rounds"].zip(
players.map { |g| g.first.last }.
compact.
map(&:values).
transpose.
map { |arr| arr.reduce(:+) }
).to_h
#=> {"scores"=>4, "strokes"=>881, "rounds"=>18}
The steps (starting from b above) are:
c = b.map(&:values)
#=> [[ 2, 146, 3],
# [ 1, 145, 3],
# [ 0, 144, 3],
# [ 0, 144, 3],
# [ 5, 162, 3],
# [-4, 140, 3]]
d = c.transpose
#=> [[ 2, 1, 0, 0, 5, -4],
# [146, 145, 144, 144, 162, 140],
# [ 3, 3, 3, 3, 3, 3]]
totals = d.map { |arr| arr.reduce(:+) }
#=> [4, 881, 18]
e = ["scores", "strokes", "rounds"].zip(totals)
#=> [["scores", 4], ["strokes", 881], ["rounds", 18]]
e.to_h
#=> {"scores"=>4, "strokes"=>881, "rounds"=>18}
Use this code:
#total= 0
players.each do |x|
a= x.values[0]
if a.class == Hash
#total += a["strokes"]
end
end
puts #total

Mapping the index of values in an array that match a specific value?

Disclaimer, I'm a beginner.
I have an array that is 16 digits, limited to 0's and 1's. I'm trying to create a new array that contains only the index values for the 1's in the original array.
I currently have:
one_pos = []
image_flat.each do |x|
if x == 1
p = image_flat.index(x)
one_pos << p
image_flat.at(p).replace(0)
end
end
The image_flat array is [0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
With the code above, one_pos returns [3, 3] rather than the [3, 5] that I'd expect.
Where am I going wrong?
Where am I going wrong?
When you call
image_flat.index(x)
It only returns first entry of x in image_flat array.
I guess there are some better solutions like this one:
image_flat.each_with_index do |v, i|
one_pos << i if v == 1
end
Try using each_with_index (http://apidock.com/ruby/Enumerable/each_with_index) on your array.
image_flat = [0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
one_pos = []
image_flat.each_with_index do |value, index|
if value == 1
one_pos << index
end
end
I think this is the most elegant solution here:
image_flat.each_index.select{|i| image_flat[i] == 1}
Here is a solution if you are looking for a solution that doesn't reach out of the enumerable block although it does require a chained solution.
image_flat.each_with_index.select { |im,i| im==1 }.map { |arr| arr[1] }
Its chained and will require an additional lookup so Gena Shumilkin's answer will probably be more optimal for larger arrays.
This was what I originally thought Gena Shumilkin was trying to reach until I realized that solution used each_index instead of each_with_index.

How to do find_index for multiple values in a Ruby array?

Given an array [0, 0, 1, 0, 1], is there a built-in method to get all of the indexes of values greater than 0? So, the method should return [2, 4].
find_index only returns the first match.
Working in Ruby 1.9.2.
In Ruby 1.8.7 and 1.9, iterator methods called without a block return an Enumerator object. So you could do something like:
[0, 0, 1, 0, 1].each_with_index.select { |num, index| num > 0 }.map { |pair| pair[1] }
# => [2, 4]
Stepping through:
[0, 0, 1, 0, 1].each_with_index
# => #<Enumerator: [0, 0, 1, 0, 1]:each_with_index>
_.select { |num, index| num > 0 }
# => [[1, 2], [1, 4]]
_.map { |pair| pair[1] }
# => [2, 4]
I would do
[0, 0, 1, 0, 1].map.with_index{|x, i| i if x > 0}.compact
And if you want that as a single method, ruby does not have a built in one, but you can do:
class Array
def select_indice &p; map.with_index{|x, i| i if p.call(x)}.compact end
end
and use it as:
[0, 0, 1, 0, 1].select_indice{|x| x > 0}

How to sort a ruby array in ascending order but keep zero last

I am trying to sort a Ruby array with the following function
#prices = #item.prices.sort { |x,y| x.total <=> y.total }
Which orders from the lowest to the highest cost. However some products have a total of 0.00 and I want them to appear last rather than at the top.
I have tried a few things but would like some way to modify this block to sort zero at the bottom but keep the rest in ascending order.
Thanks.
Try this out, I think it is doing what you request:
#prices = #item.prices.sort {|a,b| a.total==0 ? 1 : b.total==0 ? -1 : a.total<=>b.total}
Just for the record:
>> a = [0, 1, 3, 0, 2, 5, 0, 9]
=> [0, 1, 3, 0, 2, 5, 0, 9]
>> a.sort_by { |x| x.zero? ? Float::MAX : x }
=> [1, 2, 3, 5, 9, 0, 0, 0]
On most platforms 1.0/0 will evaluate to Infinity, so you can also use this instead of Float::MAX:
>> b = [1,4,2,0,5,0]
=> [1, 4, 2, 0, 5, 0]
>> Inf = 1.0/0
=> Infinity
>> b.sort_by { |x| x.zero? ? Inf : x }
=> [1, 2, 4, 5, 0, 0]
prices = [0, 1, 2, 0,4, 3]
prices = prices.sort_by do |price|
[
if price == 0
1
else
0
end,
price
]
end
p prices
# => [1, 2, 3, 4, 0, 0]
The trick here is that arrays are compared by comparing their first elements, but if those elements are equal, then by comparing their next elements, and so on. So having the sort_by block yield an array lets you determine primary sort order, secondary sort order, and so on in a clean manner.
So devise a comparator to do that ...
if x.total == 0
# always consider 0 "largest" and no 0 can be larger than another
# (make sure 0.0 is 0 and not a number really close to 0)
# perhaps x or y should be first for other reasons as well?
1
else
# otherwise lower to higher as normal
x.total <=> y.total
end
Or without comments:
foo.sort {|x, y| if x.total == 0 then 1 else x.total <=> y.total end}
Happy coding.
This would feel less hacky and less write-only to me:
prices = prices.sort_by do |price|
zero_status = price.zero? ? 1 : 0
[zero_status, price]
end
because it's an idiomatic way of sorting something by two criteria, which is what you're doing here.

Resources