Q.All the branches of XYZ school conducted an aptitude test for all the students in the age group 14 - 16. There were a total of n students. The marks of n students are stored in a list. Write a program using a user defined function that accepts a list of marks as an argument and calculates the 'xth' percentile (where x is any number between 0 and 100).You are required to perform the following steps to be able to calculate the 'xth' percentile.
Note: Percentile is a measure of relative performance i.e. It is calculated based on a candidate's performance with respect to others. For example : If a candidate's score is in the 90th percentile, that means she/he scored better than 90% of people who took the test.
Steps to calculate the xth percentile:
I. Order all the values in the data set from smallest to largest using Selection Sort. In general any of the sorting methods can be used.
II. Calculate index by multiplying x percent by the total number of values, n. For example: to find 90th percentile for 120 students: 0.90*120 = 108
III. Ensure that the index is a whole number by using math.round()
IV. Display the value at the index obtained in Step 3.
The corresponding value in the list is the xth percentile.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →A percentile is a position statistic. Sort the marks ascending (selection sort), turn the percentage into a whole-number position with index = round(x/100 * n), and read off the value standing at that position. The function below does exactly the four prescribed steps.
Concept understanding
The mean tells you the average score; a percentile tells you the rank. Saying a candidate is at the 90th percentile means roughly 90% of the candidates scored at or below that value. So the value we want is not computed arithmetically from all the marks — it is selected from the ordered list.
That is why Step I is a sort: until the marks are in order, "the 90% position" has no meaning. Once sorted, the xth percentile is simply the mark standing at position x% of n.
The four steps
| Step | What it does | In code |
|---|---|---|
| I | Order all values smallest → largest (selection sort) | nested loop finding the minimum of the unsorted part |
| II | Turn the percentage into a position: x/100 × n | x / 100 * n |
| III | Make that position a whole number | round(...) |
| IV | Display the value standing at that position | data[index - 1] |
Step III gives a 1-based position (the 9th smallest mark), but Python lists are 0-based. So the value at position index is data[index - 1]. Forgetting this off-by-one is the single most common mistake in this program.
Program
def selection_sort(data):
'''Step I - sort the marks in ascending order.'''
n = len(data)
for i in range(n - 1):
min_index = i
for j in range(i + 1, n):
if data[j] < data[min_index]:
min_index = j
if min_index != i:
data[i], data[min_index] = data[min_index], data[i]
return data
def percentile(marks, x):
'''Return the xth percentile of the list 'marks' (0 <= x <= 100).'''
if not marks:
return None
if x < 0 or x > 100:
print('x must lie between 0 and 100')
return None
data = selection_sort(marks[:]) # work on a copy - Step I
n = len(data)
index = round(x / 100 * n) # Steps II and III
if index < 1: # guard for very small x
index = 1
if index > n: # guard for x = 100
index = n
return data[index - 1] # Step IV (1-based -> 0-based)
# ---- driver ----
marks = [55, 72, 48, 90, 61, 83, 79, 66, 95, 58] # n = 10 students
print('Marks :', marks)
print('Sorted marks :', selection_sort(marks[:]))
for x in (25, 50, 90, 100):
print('{}th percentile : {}'.format(x, percentile(marks, x)))
Output
Marks : [55, 72, 48, 90, 61, 83, 79, 66, 95, 58]
Sorted marks : [48, 55, 58, 61, 66, 72, 79, 83, 90, 95]
25th percentile : 61
50th percentile : 66
90th percentile : 90
100th percentile : 95
Dry run — the 90th percentile of the sample list
Step I — selection sort (n = 10)
| Pass | List at end of pass |
|---|---|
| 1 | 48 72 55 90 61 83 79 66 95 58 |
| 2 | 48 55 72 90 61 83 79 66 95 58 |
| 3 | 48 55 58 90 61 83 79 66 95 72 |
| 4 | 48 55 58 61 90 83 79 66 95 72 |
| 5 | 48 55 58 61 66 83 79 90 95 72 |
| 6 | 48 55 58 61 66 72 79 90 95 83 |
| 7 | 48 55 58 61 66 72 79 90 95 83 |
| 8 | 48 55 58 61 66 72 79 83 95 90 |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.