-1
Hello, I have an array of values:
values = [3, 5, 12, 1, 2]
I would like in the first iteration all elements of the array to be subtracted by the first element (Ex, 5-3, 12-3, 1-3, 2-3)
# [2, 9, -2, -1]
In the second iteration (Ex, 12-5, 1-5, 2-5)
# [7, -4, -3]
In the third iteration (Ex, 1-12, 2-12)
# [-11, -10]
And so on. Subtraction must occur from the later element. I have done the following:
values.map do |price|
values.map do |value|
value - price
end
end
# [[0, 2, 9, -2, -1], [-2, 0, 7, -4, -3], [-9, -7, 0, -11, -10], [2, 4, 11, 0, 1], [1, 3, 10, -1, 0]]
I need: [2, 9, 7,-2, -1, 7, -4, -3, -11, -10, 1]
I leave my thanks for all help, tip and attention so that I can accomplish the desired operation. Thank you!
From what I understand at each iteration you take the first element of the array, subtract it from each of the other elements and remove it from the array. Although you have said "and so on" I assume the iteration stops when a single element remains in the array. You haven’t explained how to treat two-dimensional arrays and whether what you set as "need" is the expected result for the existing entry after the "#".
– anonimo