0

I tried sorting this challenge in so many ways but this time apparently my script is sorting the wrong way this time.

The challenge consists in you inputing the students name and score then the script must output the name of the student with the second lowest score, and if there's students with the score as the same value, output their names as well. But apparently it is sorting the wrong way.

This is my code:

score_list = []
for n in range(int(input())):
    name = input()
    score = float(input())
    st_score = [name, score]
    score_list.append(st_score)
sortedscore = sorted(score_list, key=lambda x: x[1])
secondguy = []
second_low = sortedscore[1][1]
for i in range(len(sortedscore)):
     if sortedscore[i][1] == second_low:
        secondguy.append(sortedscore[i])
for j in range (len(secondguy)):
    student = secondguy[j][0]
    print(student)

The given input was: 5 Harry 37.21 Berry 37.21 Tina 37.2 Akriti 41 Harsh 39

Expected Output was: Berry Harry

But actually the returned output was: Harry Berry

And it is sorting the elements this way: [['Tina', 37.2], ['Harry', 37.21], ['Berry', 37.21], ['Harsh', 39.0], ['Akriti', 41.0]]

So what exactly is making this script dont give the correct response?

6
  • 2
    The instructions say: If there are multiple students with the second lowest grade, order their names alphabetically and print each name on a new line. It doesn't look like you made any attempt to sort the names alphabetically. Commented Apr 15, 2024 at 22:30
  • you can use key=lambda x: x[1],x[0] to sort by value and name. You could also keep it as dictionary with lists { 37.21: ["Harry", "Berry"], ...} Commented Apr 16, 2024 at 2:08
  • it would be more readable if you would use for item in sortedscore: without range(len(..)) Commented Apr 16, 2024 at 2:10
  • it would be simpler to print() names directly in second loop without creating list secondguy and using third loop. Commented Apr 16, 2024 at 2:12
  • small correction: it needs () - key=lambda x:(x[1],x[0]) Commented Apr 16, 2024 at 2:18

1 Answer 1

0

When there are many students with the same result then it needs sorted by names. You get order Harry, Berry but it needs Berry, Harry

If you would use key=lambda x: (x[1],x[0]) then it will sort by score and also by name

sortedscore = sorted(score_list, key=lambda x: (x[1],x[0]))

And if you would keep it as (score, name) then you wouldn't need key

score_list = []

n = int(input())
for _ in range(n):
    name = input()
    score = float(input())
    score_list.append( (score,name) )

sortedscore = sorted(score_list))

second_low = sortedscore[1][0]
for score , name in sortedscore:
     if score == second_low:
        print(name)
Sign up to request clarification or add additional context in comments.

Comments

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.