Python Custom Sort Solution With Explanation
\nclass Solution:\n    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n        def sorting_algorithm(log):\n\t\t\t# Is a numerical log, make sure thes
```\nclass Solution:\n    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n        def sorting_algorithm(log):\n\t\t\t# Is a numerical log, make sure these entries appear on the right side without further sorting.\n\t\t\tif log[-1].isnumeric():\n\t\t\t\t# A tuple of one element. One element tuples need a trailing comma so they are not confused with a simple one (1) or 1 by python.\n\t\t\t\treturn (1,)\n\t\t\t\n\t\t\t# Is an alphabetical log, use 0 so they are always to the left of the numerical logs, \n\t\t\t# then use the more complex sorting rules for just the alphabetical logs.\n            left_side, right_side = log.split(" ", 1)\n            return (0, right_side, left_side)\n\n\t\t# Sort the logs according to the function we defined.\n        return sorted(logs, key=sorting_algorithm)\n```\nO(nlogn * m) time complexity where n is the number of strings and m is the max length of the strings. The .split() function operates with O(m) time within each string. The sorting function operates with O(nlogn) time for the strings.\n\nO(mlogn) space complexity where n is the number of strings and m is the max length of the strings. The built in python sorting algorithm uses log(n) space, holding onto the split logs uses up O(m) space for each sorting step.\n\n\nSame idea as above, this approach is explained with a long description below:\n```\nclass Solution:\n    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n        def sorting_algorithm(log):\n            left_side, right_side = log.split(" ", 1)\n\n            if right_side[0].isalpha():\n                return (0, right_side, left_side)\n            else:\n                return (1,)\n\n        return sorted(logs, key=sorting_algorithm)\n```\n\nWe split each log into a list with two entries so the following log:\n`"dig1 8 1 5 1"`\nTurns into the following:\n`["dig1", "8 1 5 1"]`\n\nThe log.split(" ", 1) means to only at most one time, from left to right, upon encountering a white-space character, split the string into two strings and store the two strings in a list.\n\nThen we look at the second string, or the right_side in this case of "8 1 5 1", we can see that the first character of 8, which is from right_side[0] is not an alpha character, with .isalpha() only returning true when a character is any lowercase or uppercase character from "a-z".\n\nFor a log like "let1 art can", right_side[0].isalpha() would be applied to the letter \'a\' in this case, and would return true. \n\nWhat we are returning from this function informs the sorted() function how sorted() ought to behave. The return of (0, right_side, left_side) tells sorted() to sort this entire log with a priority of 0, which is lower in value than 1, so any log with a right_side that begins with a letter will come before any log with a right_side that begins with a number.\n\nAfter ensuring that the logs with letters in the right_side come first, the letter logs are further sorted alpha-numerically by the contents of their entire right side, then if the right_sides still match, they are sorted even further based on the contents of their left_sides, alpha-numerically with the sorted() function.\n\nThe second return statement is missing instructions on how to be sorted with only (1,) passed into it, missing the second argument. The logs with digits in their right_side only know that they must come after the letter logs, and they do not sort because they have no rule to sort by in the second argument. \n\nWe have to write it as (1, ) because this is a tuple, I think of it as just (1), but we cannot write (1) because Python would think of it as just 1, rather than as a tuple with one element. We are specifying the sorting rules for the sort algorithm by returning tuples.\n\nThe logs are then sorted() with the sorting logic provided by sorting_algorithm. The key in sorted() is an optional argument that allows us to attach our own custom function to override the default sorting behaviour. \n\nThe built in sorting algorithm is used, which is TimSort in Python, most relevant sorting algorithms including this one provide a worst case run time of O(nlog(n)) and a space of O(n).\n\n\n
['Python', 'Python3']