We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
@itsbruce the code is neither ugly or a mess. It is solving an problem as efficiently as possible. That is, with one iteration. All the other solutions I see on this page are sorting the inputs after the are obtained, which has extra cost. When people are asking me these types of questions in interviews, they are looking for optimized code.
I came up with almost the exact same listing:
def GetName(pair):
return pair[0]
def GetGrade(pair):
return pair[1]
if __name__ == '__main__':
lowest=[]
runnersUp = []
for _ in range(int(input())):
name = input()
score = float(input())
pair = [name, score]
if not lowest:
lowest.append(pair)
elif lowest[0][1] == score:
lowest.append(pair)
elif lowest[0][1] > score:
runnersUp = list(lowest)
lowest.clear()
lowest.append(pair)
elif not runnersUp:
runnersUp.append(pair)
elif runnersUp[0][1] == score:
runnersUp.append(pair)
elif runnersUp[0][1] > score:
runnersUp.clear()
runnersUp.append(pair)
runnersUp = sorted(runnersUp, key=GetName)
for pair in runnersUp:
print(pair[0])
Nested Lists
You are viewing a single comment's thread. Return to all comments →
@itsbruce the code is neither ugly or a mess. It is solving an problem as efficiently as possible. That is, with one iteration. All the other solutions I see on this page are sorting the inputs after the are obtained, which has extra cost. When people are asking me these types of questions in interviews, they are looking for optimized code.
I came up with almost the exact same listing: