0

I have this list:

def grades = [5,4,3,2,1,1]

Where index is a grade, and value is an occurrence of the grade:

Grade Occurrence
0 5
1 4
2 3
3 2
4 1
5 1

How can I calculate the 90th percentile for the grades?

  • Does this answer your question? [Calculate percentile from a long array?](https://stackoverflow.com/questions/41413544/calculate-percentile-from-a-long-array) – geobreze Aug 02 '21 at 16:54
  • 2
    Please add the code you have tried and how it failed (e.g. errors, stacktraces, logs, ...) so we can improve on it. – cfrick Aug 02 '21 at 17:07

2 Answers2

0

If I understand correctly, a total of 16 grades have been given (5 + 4 + 3 + 2 + 1 + 1 = 16). 90 % are 14.4 grades, so discard the lowest 14 grades and take the smallest remaining (in your example it will be 4).

How to code? There are some ways:

  1. You may count through the array you have got. Subtract 5 from 14 (= 9), then 4, then 3, then 2. Once you reach zero, you’re at the index of the 90th percentile.
  2. Maybe easier to understand, but will require a few more code lines: Put all 16 grades into an array (or list): [0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5]. Since the array is sorted, the median is found at index 14.

I believe you will learn the most from coding it yourself, so I am not taking that pleasure or learning away from you.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

This gets me a whole grade. I was hoping to find an exact value of 90th percentile, but this will do for me.

def grades = [5,4,3,2,1,1]

sum=grades.sum()

per_grade=0
per_value=sum*0.9
 grades.eachWithIndex { grade_count, grade ->
  per_value-=grade_count
   if (per_value<0){per_grade=grade-1}
   if (per_value==0){per_grade=grade}
 }
out.write(per_grade)