-3

Given the names and grades for each student in a class of N students, store them in a nested list and print the name(s) of any student(s) having the second lowest grade.

Note: If there are multiple students with the second lowest grade, order their names alphabetically and print each name on a new line.

Example

 reords = [['chi', 20.0], ['bela', 50.0], ['alpha', 50.0]]

The ordered list of scores is [20.0, 50.0], so the second lowest score is 50.0 There are two students with that score:['beta', 'alpha'] . Ordered alphabetically, the names are printed as:

Input format:

The first line contains an integer, N, the number of students. The 2N subsequent lines describe each student over 2 lines.

  • The first line contains a student's name.
  • The second line contains their grade.

Output Format

Print the name(s) of any student(s) having the second lowest grade in. If there are multiple students, order their names alphabetically and print each one on a new line.

Example of input: Sample Input 0

 5
 Harry
 37.21
 Berry
 37.21
 Tina
 37.2
 Akriti
 41
 Harsh
 39

Sample Output 0

 Berry
 Harry

This my proposition code:

if __name__ == '__main__':
    student = []
    values = []
    names =[]
    for _ in range(int(input())):
        name = input()
        score = float(input())
        student.append([name, score])

    for item in student:
        values.append(item[1])
    values=list(set(values))
    values.sort()

    for item in student:
        if item[1] == values[1]:
            names.append(item[0])
    names.sort()

    for name in names:
        print(name)

when I run the code on jupiter Notebook I have the same resume. But when I run the same code in the éditor of the test I have an error. I don't understand enter image description here

Community
  • 1
  • 1

2 Answers2

0

The algorithm is:

  • Add all students with their marks to list reords
  • Sort list with unique marks (using set)
  • Filter list to get a student with the second min mark
  • Print name for each filtered student

Tested here:

if __name__ == '__main__':
    reords = []
    
    for _ in range(int(input())):
        name = input()
        score = float(input())
        reords.append([name, score])
 
    mark = sorted(set(map(lambda x: x[1], reords)))[1]
    for n, m in sorted(filter(lambda x: x[1] == mark, reords)):
        print(n)
entithat
  • 324
  • 6
  • 18
0
student = []
marks = []
names = []
for _ in range(int(input())):
    name = input()
    score = float(input())
    student.append([name, score])

for item in student:
    marks.append(item[1])
marks = list(set(marks))
marks.sort()

for item in student:
    if marks[1] == item[1]:
        names.append(item[0])
names.sort()

for name in names:
    print(name)
  • Hi Abhilash, thanks for contributing an answer. It's good practice to include a brief explanation of your code in your answer so that future readers can quickly understand how it all works. – Kyle F Hartzenberg Nov 13 '22 at 21:53