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.
This is a list comprehension. It creates a new list based on the contents of another sequence (top2). In this case, it returns a list containing all the elements in top2 which are less than score (preserving the original order). The other comprehension returns a list with all the elements which are greater than score. Meanwhile, [score] is a list containing the single value score.
So what I do in that line is create three lists - everything less than score, [score], everything bigger than score - and concatenate them into one list. What it does is take a list which may already contain scores sorted in order and adds a new item in the correct position within the list.
By doing it this way, I don't have to check to see if the list is empty or contains only one item or contains two items. I don't have to create the different strategies to cope with those 3 different possibilities. This simple strategy works in all of those situations. For example, if top2 is empty, the two list comprehensions will return empty results and two empty lists will be concatenated onto a list containing only score, which is what I would want to do if top2 were empty. If score is lower than any of the current scores in top2, then the first list comprehension will be empty and the second one will contain everything from top2; an empty list will have score added to the end of it and then the contents of top2 added to the end of that.
Oh, and I also don't have to deal with the situation where the new score has the same value as one of the scores already in top2. can you see why?
So this one line always does the right thing, no matter how many values are already in top2 and no matter how the new score value compares to them. So it works much better than a long sequence of if/else statements. It's also much clearer in its intent than a long chain of if/else - if you understand list comprehensions.
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. OK, first line...
This is a list comprehension. It creates a new list based on the contents of another sequence (top2). In this case, it returns a list containing all the elements in top2 which are less than score (preserving the original order). The other comprehension returns a list with all the elements which are greater than score. Meanwhile, [score] is a list containing the single value score.
So what I do in that line is create three lists - everything less than score, [score], everything bigger than score - and concatenate them into one list. What it does is take a list which may already contain scores sorted in order and adds a new item in the correct position within the list.
By doing it this way, I don't have to check to see if the list is empty or contains only one item or contains two items. I don't have to create the different strategies to cope with those 3 different possibilities. This simple strategy works in all of those situations. For example, if top2 is empty, the two list comprehensions will return empty results and two empty lists will be concatenated onto a list containing only score, which is what I would want to do if top2 were empty. If score is lower than any of the current scores in top2, then the first list comprehension will be empty and the second one will contain everything from top2; an empty list will have score added to the end of it and then the contents of top2 added to the end of that.
Oh, and I also don't have to deal with the situation where the new score has the same value as one of the scores already in top2. can you see why?
So this one line always does the right thing, no matter how many values are already in top2 and no matter how the new score value compares to them. So it works much better than a long sequence of if/else statements. It's also much clearer in its intent than a long chain of if/else - if you understand list comprehensions.