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.
Hi itsbruce. I read your explanations and understood the code. I was wondering, if i use the same comprehension to sort the names, will the code be better?
I modified the if statement,
if logScore(score):
scores[score] = scores.get(score,[]) + [name]
to
if logScore(score):
temp = scores.get(score,[])
scores[score] = [x for x in temp if x < name] + [name] + [x for x in temp if x >name]
and avoided sorted() function in the end.
full code:
scores = {}
top2 = []
def logScore(score):
global top2
xs = [x for x in top2 if x < score] + [score] + [x for x in top2 if x > score]
if len(xs) > 2:
scores.pop(xs[2], None)
xs.pop()
top2 = xs
return score <= xs[-1]
for _ in range(int(raw_input())):
name = raw_input()
score = float(raw_input())
if logScore(score):
temp = scores.get(score,[])
scores[score] = [x for x in temp if x < name] + [name] + [x for x in temp if x >name]
for name in scores[top2[1]]:
print name
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Nested Lists
You are viewing a single comment's thread. Return to all comments →
Hi itsbruce. I read your explanations and understood the code. I was wondering, if i use the same comprehension to sort the names, will the code be better?
I modified the if statement,
to
and avoided sorted() function in the end. full code: