Created
February 8, 2022 20:26
-
-
Save amarjitdhillon/1c0caed20275c8df29ed689652ccfc88 to your computer and use it in GitHub Desktop.
Reorder Data in Log Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def reorderLogFiles(self, logs: List[str]) -> List[str]: | |
result, plogs = [], [] | |
for i, log in enumerate(logs): | |
lg = log.split(" ") # split the string over the space and convert to a list. | |
if lg[1].isalpha(): # Identify the type of log as letter log or digit log : this can be checked if 2nd element is string or a digit? | |
# tuple with 4 keys (k1,k2,k3,k4) | |
# k1 for which type of logs, k2 for contents of logs, k3 for log identifier and k4 for original index in logs list | |
plogs.append((0,lg[1:], lg[0],i)) | |
else: | |
plogs.append((1,None, None,i)) # here k2 and k3 are not required as we do not need to sort if it's a digit | |
plogs = sorted(plogs) | |
for plog in plogs: | |
index = plog[3] # get the index of this string in the original logs array | |
result.append(logs[index]) # add the string to result list based on the index we got | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment