KnowledgeBoat Logo
|

Computer Science

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:

  1. 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.
  2. 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.
  3. Ensure that the index is a whole number by using math.round().
  4. Display the value at the index obtained in Step 3.

The corresponding value in the list is the xth percentile.

Python Sorting

2 Likes

Answer

def selection_sort(arr):
    n = len(arr)
    for i in range(n - 1):
        min_idx = i
        for j in range(i + 1, n):
            if arr[j] < arr[min_idx]:
                min_idx = j
        arr[i], arr[min_idx] = arr[min_idx], arr[i]

def calculate_percentile(marks_list, percentile):
    selection_sort(marks_list)
    n = len(marks_list)
    index = round(percentile * n / 100) - 1 
    if index >= 0 and index < n:
        return marks_list[index]
    else:
        return None  
    
marks = eval(input("Enter list of marks"))
xth_percentile = int(input("Enter percentile to be calculated: "))
result = calculate_percentile(marks, xth_percentile)

if result is not None:
    print("The", xth_percentile, "th percentile is:", result)
else:
    print("Invalid percentile.")
Output
Enter list of marks[35, 25, 45, 50, 60, 75, 80]
Enter percentile to be calculated: 70
The 70 th percentile is: 60

Answered By

1 Like


Related Questions