-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

5
  • I am pretty sure a verry similar quesiton was already asked here Commented Apr 19, 2021 at 12:41
  • The error is saying there are certain inputs that your code doesn't work with. Test it with the full input in your screenshot and see what happens. Commented Apr 19, 2021 at 12:42
  • I don't know, but I want to have more explanation to this problem. please look the image to see what I want to talk about Commented Apr 19, 2021 at 12:43
  • Thank's, Peter. let me see and run again Commented Apr 19, 2021 at 12:45
  • Possible Duplicate? Commented Apr 19, 2021 at 12:46

5 Answers 5

1

My solution is:

  1. filling the nested list with records
  2. sorting the list in the ascending order of scores
  3. finding the lowest score
  4. finding the names with the second lowest score - the second loop
  5. sorting and printing the names in the alphabetical order
if __name__ == "__main__":
    # filling the nested list with records
    records = []
    for _ in range(int(input())):
        name = input()
        score = float(input())
        records.append([name, score])

    # sorting the list in the ascending order of scores
    sort_rec = sorted(records, key=lambda x: x[1])

    # this is the lowest score
    lowest_score = sort_rec[0][1]
    second_lowest_score = None
    names = []

    # looping over all the scores in sort_rec[1:]
    # beginning from the 1st (pythonic indexation)
    for item in sort_rec[1:]:
        # candidate value for the second lowest score
        cand_val = item[1]
        if cand_val > lowest_score:
            # checking if the candidate values is the same as the
            # second lowest score
            # of if the second lowest score is different from None
            if cand_val == second_lowest_score or second_lowest_score is None:
                names.append(item[0])
                second_lowest_score = cand_val
            else:
                break

    # sorting the names in the alphabetical order and printing
    [print(name) for name in sorted(names)]
Sign up to request clarification or add additional context in comments.

Comments

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)

5 Comments

can you publish your code please, juste to see?
@AmounguiSerge, you can see it in my answer :) However, pastebin.com/fVa5zb2x
Cool, your code code work correctly. that's right. thank's for your help.
I find you very experienced, how to stay in contact in order to exchange regularly. because I like your way of code, I would like to reach this level
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)

1 Comment

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.
-1
nested_list=[]
for _ in range(int(input())):
    name = input()
    score = float(input())
    nested_list+=[[score, name]]
nested_list.sort()

for i in nested_list:
    if i[0]>nested_list[0][0]:
        second_lowest_grade= i[0]
        break;

student_names=[i[1] for i in nested_list if i[0]==second_lowest_grade]
student_names.sort()
for student_name in student_names:
    print(student_name)

Comments

-1
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)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.