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'm just trying to piece this all together by myself and I could be entirely wrong, but here I go.
"key" is setting values for each element so as to use them for comparison and subsequently sorting later on.
Each parameter in our lambda function contributes to the value of key - the order in which the parameters are aranged has to do with this but I don't know how (help please :D)
ord(c) finds the unicode point of the character we're looking at
We use >> to shift the ordinal value to less than 2 bits (so between 0 and 3 in decimal)
We set the result of this to a negative because we sort in ascending order
Basically -ord(c) >> 5 sets the values to -3 if it's a lowercase character, -2 if it's an uppercase character and -1 if it's a digit. This segregates each section of our string into uU12. From here we need to sort the numbers, so we use c in '02468' to do so. And then we use c at the end to sort the contents of each group by value.
At least that's how I think it all works. I'll wait for someone to correct me :)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
ginortS
You are viewing a single comment's thread. Return to all comments →
I'm just trying to piece this all together by myself and I could be entirely wrong, but here I go.
Basically -ord(c) >> 5 sets the values to -3 if it's a lowercase character, -2 if it's an uppercase character and -1 if it's a digit. This segregates each section of our string into uU12. From here we need to sort the numbers, so we use c in '02468' to do so. And then we use c at the end to sort the contents of each group by value.
At least that's how I think it all works. I'll wait for someone to correct me :)