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

Caleb Faruki
sum = @products.inject(0){ |sum,item| sum += item['count'] }
@selected = @products.select { |item| (item['count']/sum) >= 0.05 }

I want to select every element from the @products array whose count property is greater than 5% of the sum. @products is an array of hashes.

However, when I use this second line, @selected returns an empty array. After finding no fault with |item| or the @products array itself, I'm inclined to believe it has something to do with trying to use an external variable, sum, inside the .select block. Could someone explain to me why @selected is returning nothing?

Niklas B.

If the counts are integers, item['count']/sum will always be zero due to integer division.

Try the following instead:

@selected = @products.select { |item| item['count'] >= 0.05 * sum }

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

MySQL select records with sum greater than threshold

From Dev

Select items where their amount is greater than 5

From Dev

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

From Dev

sum if greater than in r

From Dev

sql select where (sum of 2 columns) is greater than X

From Dev

How to select where sum of fields is greater than a value in MongoDB

From Dev

SQL select all records only if the sum is greater than 0

From Dev

Is it possible to select all elements with an attribute value greater than a certain number?

From Dev

Is it possible to select all elements with an attribute value greater than a certain number?

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

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

From Dev

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

From Dev

Quick comparison of numpy array elements, greater or less than each other

From Dev

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

From Dev

PostgreSQL: Replace values greater than a certain limit in array elements

From Dev

Can we use IntStream#sum, If sum of elements is greater than the Integer.MAX_VALUE?

From Dev

Get elements of a matrix that are greater than sum of their two indices in row major order

From Dev

Get elements of a matrix that are greater than sum of their two indices in row major order

From Dev

Select columns thats greater than?

From Dev

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

From Dev

maximum no. of elements in an array having sum less than or equal to k

From Dev

maximum no. of elements in an array having sum less than or equal to k

From Dev

Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

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?

From Dev

numpy.argmin for elements greater than a threshold

From Dev

How to find the elements greater than integer

Related Related

  1. 1

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

  2. 2

    MySQL select records with sum greater than threshold

  3. 3

    Select items where their amount is greater than 5

  4. 4

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

  5. 5

    sum if greater than in r

  6. 6

    sql select where (sum of 2 columns) is greater than X

  7. 7

    How to select where sum of fields is greater than a value in MongoDB

  8. 8

    SQL select all records only if the sum is greater than 0

  9. 9

    Is it possible to select all elements with an attribute value greater than a certain number?

  10. 10

    Is it possible to select all elements with an attribute value greater than a certain number?

  11. 11

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

  12. 12

    Remove all elements from array greater than n

  13. 13

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

  14. 14

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

  15. 15

    Quick comparison of numpy array elements, greater or less than each other

  16. 16

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

  17. 17

    PostgreSQL: Replace values greater than a certain limit in array elements

  18. 18

    Can we use IntStream#sum, If sum of elements is greater than the Integer.MAX_VALUE?

  19. 19

    Get elements of a matrix that are greater than sum of their two indices in row major order

  20. 20

    Get elements of a matrix that are greater than sum of their two indices in row major order

  21. 21

    Select columns thats greater than?

  22. 22

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

  23. 23

    maximum no. of elements in an array having sum less than or equal to k

  24. 24

    maximum no. of elements in an array having sum less than or equal to k

  25. 25

    Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

  26. 26

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

  27. 27

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

  28. 28

    numpy.argmin for elements greater than a threshold

  29. 29

    How to find the elements greater than integer

HotTag

Archive