I am trying to solve a problem in coding websites. I came up with below problem.
Problem: Given the names and grades for each student in a class of 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
The ordered list of scores is , so the second lowest score is . There are two students with that score: . Ordered alphabetically, the names are printed as:
alpha beta Input Format
The first line contains an integer, , the number of students. The subsequent lines describe each student over lines.
- The first line contains a student's name.
- The second line contains their grade.
Constraints
There will always be one or more students having the second lowest 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.
Sample Input 0
5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
Sample Output 0
Berry
Harry
Explanation 0
There are students in this class whose names and grades are assembled to build the following list:
python students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]
The lowest grade of belongs to Tina. The second lowest grade of belongs to both Harry and Berry, so we order their names alphabetically and print each name on a new line.
Reference link: codingurl
I tried below two solutions but both are failing in the locked test cases.
Solution 1:
if __name__ == '__main__':
st = []
for _ in range(int(input())):
name = input()
score = float(input())
st.append([name,score])
lowest=st[0][1]
second_lowest=st[0][1]
for student in st:
if student[1]<lowest:
second_lowest=lowest
lowest=student[1]
ls=[]
for i in range(len(st)):
if st[i][1]==second_lowest:
ls.append(st[i][0])
for i in sorted(ls):
print(i)
Solution 2:
if __name__ == '__main__':
st = []
for _ in range(int(input())):
name = input()
score = float(input())
st.append([name,score])
lowest=st[0][1]
second_lowest=st[0][1]
for student in st:
if student[1]<lowest:
second_lowest=lowest
lowest=student[1]
ls=[]
for i in range(len(st)):
if st[i][1]==lowest:
ls.append(i)
for i in ls:
del st[i]
lowest=st[0][1]
for student in st:
if student[1]<lowest:
second_lowest=lowest
lowest=student[1]
ls=[]
for i in range(len(st)):
if st[i][1]==lowest:
ls.append(st[i][0])
for i in sorted(ls):
print(i)
Can you please suggest where I am going wrong.
CodePudding user response:
Seems like you're over-complicating it by re-implementing min()
as a loop and trying to have it keep track of both the minimum and the second-minimum in a single pass (which seems like it should be vulnerable to getting fooled by local minima -- if you were to step through your code with inputs in different orderings I suspect you can figure out exactly where it goes wrong). Keep in mind that there can be ties for lowest and/or second lowest.
Just use min()
and do it in two passes:
# Get all the input as a list of (name, score).
grades = [
(input(), float(input()))
for _ in range(int(input()))
]
# Drop the lowest score.
lowest = min(score for _, score in grades)
grades = [(name, score) for name, score in grades if score > lowest]
# Drop all EXCEPT the lowest of what remains.
lowest = min(score for _, score in grades)
grades = [(name, score) for name, score in grades if score == lowest]
# Sort and print.
grades.sort()
for name, _ in grades:
print(name)
CodePudding user response:
I usually use defaultdict to address situations like this:
>>> from collections import defaultdict
>>>
>>> d = defaultdict(list)
>>> students_scores = [
... ['Harry', 37.21],
... ['Berry', 37.21],
... ['Tina', 37.2],
... ['Akriti', 41],
... ['Harsh', 39]
... ]
>>>
>>> for student, score in students_scores:
... d[score] = [student]
...
>>> second_lowest_score = sorted(d.keys())[1]
>>> second_lowest_students = sorted(d[second_lowest_score])
>>> print(second_lowest_students)
['Berry', 'Harry']
>>>