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.
I applaud your perception, but the first objection is that you've created a significantly less efficient solution. You sort the list of students with the grade you've just seen if that score is currently in the lowest two scores. Imagine if the first few students in the input get the highest score and there are more than 2 different scores. Your code will diligently sort that list, only to throw it away later. That's a waste of time.
Aside from that, reusing the technique from logScore on the list of names is likely to be significantly less efficient than Python's sorted function.
What I did in the logScore function is an insertion sort - one of the easiest sort functions to implement but one of the least efficient on data sets of any significant size. It isn't even the most efficient implementation of insertion sort - an efficient implementation would compare the next value to insert to the value just inserted, remembering the position of the value just inserted, so that the next iteration can work forward or backward from that point rather than starting from the beginning each time. That doesn't matter in logScore, because the data set never grows beyond 3 (being cropped back to a maximum of 2 elements after the sort).
As it happens, we're told there will never be more than 4 students with the second-lowest grade, so the difference in efficiency is unlikely to matter, but a) I don't see the point in dropping the core sort function for a more-than-usually-inefficient insertion sort here and b) I prefer any piece of code to do only one thing unless there's a clear and needed performance gain from doing more than one thing at once. Trying to do two things at once is also why you missed the fact that your proposed solution is inefficient.
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 →
I applaud your perception, but the first objection is that you've created a significantly less efficient solution. You sort the list of students with the grade you've just seen if that score is currently in the lowest two scores. Imagine if the first few students in the input get the highest score and there are more than 2 different scores. Your code will diligently sort that list, only to throw it away later. That's a waste of time.
Aside from that, reusing the technique from logScore on the list of names is likely to be significantly less efficient than Python's sorted function.
What I did in the logScore function is an insertion sort - one of the easiest sort functions to implement but one of the least efficient on data sets of any significant size. It isn't even the most efficient implementation of insertion sort - an efficient implementation would compare the next value to insert to the value just inserted, remembering the position of the value just inserted, so that the next iteration can work forward or backward from that point rather than starting from the beginning each time. That doesn't matter in logScore, because the data set never grows beyond 3 (being cropped back to a maximum of 2 elements after the sort).
As it happens, we're told there will never be more than 4 students with the second-lowest grade, so the difference in efficiency is unlikely to matter, but a) I don't see the point in dropping the core sort function for a more-than-usually-inefficient insertion sort here and b) I prefer any piece of code to do only one thing unless there's a clear and needed performance gain from doing more than one thing at once. Trying to do two things at once is also why you missed the fact that your proposed solution is inefficient.