'HackerRank's Nested List Problem:-Facing issues with 2 out of 10 test cases

The following is my attempt at the solution.The result needs to be ordered in lexicographical order which i did but i am not getting why the test cases which i mentioned below after the #Code aren't working.

#Code

N=int(input("enter number of students in the range of 2 to 5"))
physics_students=[]

#creating a list of student names with their grades
for i in range(N):
    name = input()
    score = float(input())
    physics_students.append([name,score])
physics_students.sort()

#Removing the lowest grade
grades_list=[]
for i in range(N):
    grades_list.append(physics_students[i][1])
grades_list.sort()
grades_list.remove(min(grades_list))

#finding out the names of students with second lowest grade
for i in range(N):
    if physics_students[i][1]==grades_list[0]:
        print("name",physics_students[i][0])

Test Cases which didn't pass are as follows:

Test Case 1: 4 abhay -50 sri -50 rakesh -50 kishore 51

Test Case 2: 5 ram 20 ramesh 20 suresh 19 venkat 19 jaydeep 21

Hacker Rank's problem link



Solution 1:[1]

The blunder's here:

grades_list=[]
for i in range(N):
    grades_list.append(physics_students[i][1])
grades_list.sort()
grades_list.remove(min(grades_list))

HackerRank Problem Descriptor
Note: If there are multiple students with the same grade, order their names alphabetically and print each name on a new line. emphasis mine

Now consider if there were multiple students with the same lowest grade. Let's say the grades were [1, 1, 2, 3]. This becomes your sorted grades_list.

Now what happens when you call .remove()?

>>> grades_list.remove(min(grades_list))
>>> print(grades_list)
[1, 2, 3]

So you see, that the min isn't fully removed.

There are several ways to solve this, but I want to leave the implementation to you since this is a pretty good exercise.

Spoiler:

Hint: Maybe try keeping only distinct elements in your grades_list?


Other Issues

N=int(input("enter number of students in the range of 2 to 5"))

There shouldn't need to be a prompt there. It'll spoil the standard output and HackerRank will mark it as wrong.

print("name",physics_students[i][0])

This also. You can remove name. : )

Solution 2:[2]

here is the short code for problem.

score_list = [];
if __name__ == '__main__':
   for _ in range(int(input())):
       name = input()
       score = float(input())
    
    
       score_list.append([name, score])
second_highest = sorted(set([score for name, score in score_list]))[1]
print('\n'.join(sorted([name for name, score in score_list if score == 
second_highest])))

Solution 3:[3]

The updated version of you code (passes all Hackerrank tests):

if __name__ == '__main__':
physics_students = []
for _ in range(int(input())):
    name = input()
    score = float(input())
    physics_students.append([name, score])
physics_students.sort()
N = len(physics_students)
### Removing the lowest grades
grades_list = []
for i in range(N):
    grades_list.append(physics_students[i][1])
grades_list.sort()
min_grade = min(grades_list)
grades_list_temp = grades_list[:]
for i in range(len(grades_list)):
    if grades_list[i] == min_grade:
        grades_list_temp.pop(0)
    else:
        pass
#### Finding out the names of students with second lowest grade
lowest2nd_names_list = []
for i in range(N):
    if physics_students[i][1] == grades_list_temp[0]:
        lowest2nd_names_list.append(physics_students[i][0])

for name in lowest2nd_names_list:
    print(name)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ankit Limone
Solution 3 bergart