BackmediumArrays Cred

Optimizing Student Rankings with Variable-Length GPA Lists Solution

Problem Statement

Given a list of students with their respective GPAs, return the top 5 students with the highest GPAs. If there are more than 5 students with the same GPA as the fifth highest GPA, include only the first 5 students with that GPA.

Example 1
Input
[['Alice', 3.9], ['David', 3.9], ['Bob', 3.8], ['Frank', 3.8], ['John', 3.8], ['Charlie', 3.8], ['Dave', 3.7], ['Emma', 3.6], ['Frankie', 3.5]]
Output
[['Alice', 3.9], ['David', 3.9], ['Bob', 3.8], ['Frank', 3.8], ['John', 3.8]]

Explanation: Step-by-step: First, sort the list of students based on their GPAs in descending order. Then, find the 5th GPA. After that, filter the list to include only the first 5 students with the same GPA as the 5th GPA.

Example 2
Input
[['David', 3.9], ['Charlie', 3.8], ['Bob', 3.7], ['Alice', 3.6], ['John', 3.5], ['Dave', 3.4], ['Emma', 3.3], ['Frankie', 3.2], ['Sam', 3.1]]
Output
[['David', 3.9], ['Charlie', 3.8], ['Bob', 3.7], ['Alice', 3.6], ['John', 3.5]]

Explanation: Step-by-step: First, sort the list of students based on their GPAs in descending order. Then, find the 5th GPA. After that, filter the list to include only the first 5 students with the same GPA as the 5th GPA.

Constraints

  • The input list will contain at least 5 students.
  • The GPA of each student will be a decimal number between 0 and 4.
  • The function should return a list of lists, where each sublist contains the name and GPA of a student.
  • If there are more than 5 students with the same GPA as the fifth highest GPA, include only the first 5 students with that GPA.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free