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 x 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.
python import math
def selection_sort(marks): n = len(marks) for i in range(n-1): min_idx = i for j in range(i+1, n): if marks[j] < marks[min_idx]: min_idx = j marks[i], marks[min_idx] = marks[min_idx], marks[i] return marks
def calculate_percentile(marks, x): # Step I: Order all values from smallest to largest sorted_marks = selection_sort(marks.copy())
# Step II: Calculate index by multiplying x percent by n
n = len(sorted_marks)
index = (x / 100) * n
# Step III: Ensure index is a whole number
index = round(index)
# Step IV: Display value at the index
return sorted_marks[index]
Main program
n = int(input("Enter number of students: ")) marks_list = [] for i in range(n): m = int(input("Enter marks: ")) marks_list.append(m)
x = float(input("Enter percentile (0-100): ")) result = calculate_percentile(marks_list, x) print(f"The {x}th percentile is: {result}")
Explanation
This question tests the student's ability to implement Selection Sort and apply mathematical operations to calculate percentiles. The program follows the four steps outlined in the textbook: sorting the data, calculating the index position, rounding to get a whole number, and retrieving the value at that index. The Selection Sort algorithm works by repeatedly finding the minimum element and placing it at the beginning of the unsorted portion.
Solution Steps
Step 1: Define selection_sort() function to arrange marks in ascending order using Selection Sort technique.
Step 2: Define calculate_percentile() function that accepts marks list and x as arguments.
Step 3: Inside calculate_percentile(), first sort the list using selection_sort().
Step 4: Calculate index using formula: index = (x/100) × n where n is total number of values.
Step 5: Round the index to get a whole number using round() function.
Step 6: Return the value at the calculated index position from the sorted list.