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.
Python newbie here - finally something I can understand!! I struggle to follow the logic of other solutions proposed here... sobs
Someone mentioned below that this code fails when you apply 8 element - can anyone explain what this mean please?
I actually made the same comment a couple of weeks ago, but I realized that since scores is a set, sorted(scores)[1]will return the second lowest score.
Pardon my knowledge of sets, but i thought they were "unordered collection with no duplicate elements" and that you could not sort them. So how can you use the sorted(scores)[1] function?
why did you use sorted(scores)[1], why not [-2]
We need 2nd highest right? then if we sort then [-2] will be the second highest
could you please explain it to me brother 🤔
Of the top solutions, this is the best IMO. I don't know why we want to encourage code that is not readable. In a real world development environment, you are supposed to write code that your peers can understand right away. Not spend 20 minutes deciphering your 1 liner. Thank you sir.
I was having initially an issue with this code because i used the sort function with the set similar to how it works for list. I love this solution but can you explain to me what is the importance of using a set? I want to know for my own knowledge and improvement.
Very neat readable code - as mentioned just anyone can understand.
For a change, liked this more than the compact snippets mentioned. Even more when it comes to debugging.
BTW, you could use the following one liner for the last code block to sort and print the names list on seperate lines-
Nested Lists
You are viewing a single comment's thread. Return to all comments →
Easy pythonic code that anyone can understand
Great use of the set function to eliminate repeating scores! I didn't know about sets and learned about its usefulness from your solution thanks!!
Thanks!
if you appiled fo 8 elment, this code will fail bro. this code only use for 5 input
How? can you explain it.
Python newbie here - finally something I can understand!! I struggle to follow the logic of other solutions proposed here... sobs Someone mentioned below that this code fails when you apply 8 element - can anyone explain what this mean please?
scores.add(score) can you please explain this statement
It's basically to add the score of each student in scores = set(), but the set will store only unique values
Thanks
This is too good, really. Thank you!
awesome
can you explain line 9 !! what is happening there?
How does your code resolve the case when multiple students share the lowest score? ex. Suppose 3 people receive grades of 0.
nvm: .add instead of .append
Thanks Dude
Hi there, I used the same code, I only replaced
second_lowest = sorted(scores)[1]
with
sorted_scores=sorted(scores) second_lowest=sorted_scores[1]
It works perfectly on spyder but here on hackerrank it file all the tests, any idea?
what if the lowest grade is for two students then the second lowest grade will be in the index 2
since you use sorted scores[1] this may not work in cases where there are 2 instances of the lowest score, other than that -- very readable code!
I actually made the same comment a couple of weeks ago, but I realized that since
scores
is a set,sorted(scores)[1]
will return the second lowest score.Great catch! Thanks for the clarification
Hey, man! Thanks a bunch for this simple, yet amazing code! I changed the lower half to make it more compact. :)
second = sorted(scores)[-2]
for name, score in marksheet: if score == second: print (name)
This will not work for N=2 and both marks being entered same.
Have a look at the constraints! N >= 2.
Thanks! This was helpful.
Thank you so much!!
We can get rid of last two lines by running for loop on sorted list
for name, score in sorted(l): if score == second_lowest: print(name)
second_lowest => re-compute this if student names have max_score probably appearence more than one time
Thank you ! Very clearly.
thank you very much for this lucid explanation
thanks you
superb man
brilliant! thanks so much ... even 3 years later.
I find this so clear and comprehensible, thank you. I still can't fully wrap my head around list comprehension.
nice bro..
@prasang_singhal clean and easy to follow. Thank you.
you cannot use add with float object
ur code is quite easy, thnx a more sorted way...
thank you sir
thank you so much the code is too easy to understand for begineers
Pardon my knowledge of sets, but i thought they were "unordered collection with no duplicate elements" and that you could not sort them. So how can you use the sorted(scores)[1] function?
What happens if you have 2 or more lowest scores? this wont work then
well, this was really easy to understand for rookie as myself
This is working bro!!!!!!!!!!
Thanks for the comment. For me as a begginer (started learning 3weeks ago) its the most understanable code!
This code is beautiful for beginners like me... explains a lot of things... thanks a ton :)
why did you use sorted(scores)[1], why not [-2] We need 2nd highest right? then if we sort then [-2] will be the second highest could you please explain it to me brother 🤔
why have you used scores.add(score) what is the need of that line
Thank you!
nice solution
the world needs more people like you
Now this is how one writes a simple code.
this is very elegant, thanks!
very helpfull
Of the top solutions, this is the best IMO. I don't know why we want to encourage code that is not readable. In a real world development environment, you are supposed to write code that your peers can understand right away. Not spend 20 minutes deciphering your 1 liner. Thank you sir.
thanks
Found this really helpful
You were so right when you said "anyone can understand" . Actually it is. Thank You.
Clear and beautiful
I was having initially an issue with this code because i used the sort function with the set similar to how it works for list. I love this solution but can you explain to me what is the importance of using a set? I want to know for my own knowledge and improvement.
Why use a set over an array is it because it cannot contain duplicates?
I was looking for a solution that didn't use dictionaries or marksheet. Your approach gave me a new way to look at the problem. Thank you!
I undertstood only this code, it is easy to read and understand with great use of sets and list with sort
Very neat readable code - as mentioned just anyone can understand. For a change, liked this more than the compact snippets mentioned. Even more when it comes to debugging.
BTW, you could use the following one liner for the last code block to sort and print the names list on seperate lines-
Elegant piece