How to check that all elements of one array are greater than their counterparts in a parallel array (in Ruby).

Darcys22

I am trying to compare two arrays to ensure that the corresponding values of one is always greater than the other.

a = [2, 3, 4]
b = [1, 2, 3]

# a[0] > b[0] ... a[x] > b[x]

At this point I am thinking of using inject with an index and return if the comparison fails, like:

b.each_with_index.inject(true) do |cmp, (element,index)|
  if element > a[index] do
    cmp = false
    return
  end
end

Is there a better way of doing this? Kinda feeling like Ruby or Rails might already have something like this built-in and I missed it.

Michael Kohl

This is what I would do:

 a.zip(b).all? { |a, b| a > b }

Note though that zip will truncate in case the two arrays are not of the same size.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change array elements that are greater than 5 to 5, in one line?

From Dev

Check if all elements of one array is in another array

From Dev

Check if at least two elements in an array are greater than zero - JavaScript/Typescript

From Java

Replace all elements of Python NumPy Array that are greater than some value

From Dev

Remove all elements from array greater than n

From Dev

Ruby: check if all array elements are equal

From Dev

Check if all elements of one array exist in other

From Dev

How to find number of elements greater/smaller than an element in an array?

From Dev

How to find number of elements greater/smaller than an element in an array?

From Dev

how to check an array if each following value is greater than the last

From Dev

Select array elements that are greater than 5% of a sum

From Dev

How to check if one value is greater than other

From Dev

How to find the number of elements in the array that are bigger than all elements after it?

From Dev

How to find the number of elements in the array that are bigger than all elements after it?

From Dev

How to make an array unique if all elements are same in array, Ruby?

From Dev

How to check if an array is one of the elements in a two-dimensional array

From Dev

Check if value of array is greater than. Than do other styling

From Dev

Is it possible to Multiply elements in the array if the elements value is greater than 10?

From Dev

Check if all elements in an array are equal

From Dev

Check if all elements in array are strings

From Dev

check if all the elements in the array are equal

From Dev

How to determine if one array contains all elements of another array in Swift?

From Dev

How to compare one array element with all others array elements in a for loop?

From Dev

How to filter array values greater than x

From Dev

How to group Ruby enumerable/array by more than one field?

From Dev

How to group Ruby enumerable/array by more than one field?

From Dev

How to count the elements in array which are more than one (php)

From Dev

How can we calculate, for every element in an array, the number of elements to the right that are greater than that element?

From Dev

How can we calculate, for every element in an array, the number of elements to the right that are greater than that element?

Related Related

  1. 1

    How to change array elements that are greater than 5 to 5, in one line?

  2. 2

    Check if all elements of one array is in another array

  3. 3

    Check if at least two elements in an array are greater than zero - JavaScript/Typescript

  4. 4

    Replace all elements of Python NumPy Array that are greater than some value

  5. 5

    Remove all elements from array greater than n

  6. 6

    Ruby: check if all array elements are equal

  7. 7

    Check if all elements of one array exist in other

  8. 8

    How to find number of elements greater/smaller than an element in an array?

  9. 9

    How to find number of elements greater/smaller than an element in an array?

  10. 10

    how to check an array if each following value is greater than the last

  11. 11

    Select array elements that are greater than 5% of a sum

  12. 12

    How to check if one value is greater than other

  13. 13

    How to find the number of elements in the array that are bigger than all elements after it?

  14. 14

    How to find the number of elements in the array that are bigger than all elements after it?

  15. 15

    How to make an array unique if all elements are same in array, Ruby?

  16. 16

    How to check if an array is one of the elements in a two-dimensional array

  17. 17

    Check if value of array is greater than. Than do other styling

  18. 18

    Is it possible to Multiply elements in the array if the elements value is greater than 10?

  19. 19

    Check if all elements in an array are equal

  20. 20

    Check if all elements in array are strings

  21. 21

    check if all the elements in the array are equal

  22. 22

    How to determine if one array contains all elements of another array in Swift?

  23. 23

    How to compare one array element with all others array elements in a for loop?

  24. 24

    How to filter array values greater than x

  25. 25

    How to group Ruby enumerable/array by more than one field?

  26. 26

    How to group Ruby enumerable/array by more than one field?

  27. 27

    How to count the elements in array which are more than one (php)

  28. 28

    How can we calculate, for every element in an array, the number of elements to the right that are greater than that element?

  29. 29

    How can we calculate, for every element in an array, the number of elements to the right that are greater than that element?

HotTag

Archive