Skip to main content
added 498 characters in body
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Your code seems reasonable, if you consider the use of nested lists to be a requirement. It is clearly written enough that you could just omit most or even all of the comments. You could simplify the determination of the second-lowest score using itertools.groupby().

It is customary to use _ as the name of a "throw-away" variable whose value will never be used, such as studentrecord in

students = [[input(), float(input())] for studentrecord in range(n)]

I wouldn't blame Python for being the wrong language for this challenge. Rather, the advice to use nested lists is poor. This problem would be much more naturally solved if you classified the students by their score. That would be easily accomplished using a dictionary, specifically a defaultdict(list).

from collections import defaultdict

students_by_score = defaultdict(list)
for _ in range(int(input())):
    name = input()
    score = float(input())
    students_by_score[score].append(name)

second_lowest_score = sorted(students_by_score)[1]
for student_name in sorted(students_by_score[second_lowest_score]):
    print(student_name)

If I had to use a nested list in the solution, I'd still convert it to a dictionary first. =)

Furthermore, I would consider a list of tuples to be more appropriate than a list of lists for this situation. A list is good for homogeneous data of variable length. A tuple, on the other hand, has a connotation of being a fixed number of elements, possibly of different data types. (Tuples in Python also happen to be immutable.) Although the nested list works just fine, and the Python interpreter doesn't care, I would choose tuples here because they would convey the right connotation.

Your code seems reasonable, if you consider the use of nested lists to be a requirement. It is clearly written enough that you could just omit most or even all of the comments. You could simplify the determination of the second-lowest score using itertools.groupby().

It is customary to use _ as the name of a "throw-away" variable whose value will never be used, such as studentrecord in

students = [[input(), float(input())] for studentrecord in range(n)]

I wouldn't blame Python for being the wrong language for this challenge. Rather, the advice to use nested lists is poor. This problem would be much more naturally solved if you classified the students by their score. That would be easily accomplished using a dictionary, specifically a defaultdict(list).

from collections import defaultdict

students_by_score = defaultdict(list)
for _ in range(int(input())):
    name = input()
    score = float(input())
    students_by_score[score].append(name)

second_lowest_score = sorted(students_by_score)[1]
for student_name in sorted(students_by_score[second_lowest_score]):
    print(student_name)

If I had to use a nested list in the solution, I'd still convert it to a dictionary first. =)

Your code seems reasonable, if you consider the use of nested lists to be a requirement. It is clearly written enough that you could just omit most or even all of the comments. You could simplify the determination of the second-lowest score using itertools.groupby().

It is customary to use _ as the name of a "throw-away" variable whose value will never be used, such as studentrecord in

students = [[input(), float(input())] for studentrecord in range(n)]

I wouldn't blame Python for being the wrong language for this challenge. Rather, the advice to use nested lists is poor. This problem would be much more naturally solved if you classified the students by their score. That would be easily accomplished using a dictionary, specifically a defaultdict(list).

from collections import defaultdict

students_by_score = defaultdict(list)
for _ in range(int(input())):
    name = input()
    score = float(input())
    students_by_score[score].append(name)

second_lowest_score = sorted(students_by_score)[1]
for student_name in sorted(students_by_score[second_lowest_score]):
    print(student_name)

If I had to use a nested list in the solution, I'd still convert it to a dictionary first. =)

Furthermore, I would consider a list of tuples to be more appropriate than a list of lists for this situation. A list is good for homogeneous data of variable length. A tuple, on the other hand, has a connotation of being a fixed number of elements, possibly of different data types. (Tuples in Python also happen to be immutable.) Although the nested list works just fine, and the Python interpreter doesn't care, I would choose tuples here because they would convey the right connotation.

Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Your code seems reasonable, if you consider the use of nested lists to be a requirement. It is clearly written enough that you could just omit most or even all of the comments. You could simplify the determination of the second-lowest score using itertools.groupby().

It is customary to use _ as the name of a "throw-away" variable whose value will never be used, such as studentrecord in

students = [[input(), float(input())] for studentrecord in range(n)]

I wouldn't blame Python for being the wrong language for this challenge. Rather, the advice to use nested lists is poor. This problem would be much more naturally solved if you classified the students by their score. That would be easily accomplished using a dictionary, specifically a defaultdict(list).

from collections import defaultdict

students_by_score = defaultdict(list)
for _ in range(int(input())):
    name = input()
    score = float(input())
    students_by_score[score].append(name)

second_lowest_score = sorted(students_by_score)[1]
for student_name in sorted(students_by_score[second_lowest_score]):
    print(student_name)

If I had to use a nested list in the solution, I'd still convert it to a dictionary first. =)