I have to find the names of student(s) with second smallest grade. My code is working fine some test cases but this one in particular is troubling me:
4 Rachel -50 Mawer -50 Sheen -50 Shaheen 51
Output returned is
Mawer Rachel Sheen
Shaheen has the 2nd smallest grade and should be the output. I am not sure where am I going wrong. Also, I am having trouble with grades as float inputs:
4 Shadab 8 Varun 8.9 Sarvesh 9.5 Harsh 10
Output thrown is Sarvesh when it should be Varun.
import heapq
# range(int(input())):
n = int(input())
builtlist = []
temp= []
names = []
for i in range(0, n):
name = input()
score = float(input())
builtlist.append([name, score])
temp = sorted(builtlist, key = lambda x: x[1])
#minvalue = min(temp, key= lambda x: x[1])
for j in range(len(temp)):
secondsmall = heapq.nsmallest(2, temp)[-1]
if (temp[j][1]==secondsmall[1]):
names.append(temp[j][0])
list = sorted(names)
print(*list, sep = "\n")
I guess there's some trouble with heapq.nsmallest method I have used but I can't figure out what it is.