aboutsummaryrefslogtreecommitdiff
path: root/reorder_data_log_files.py
blob: d6da9f72a091de0f8a6cd43413d06bd378d0fc28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 937. Reorder Data in Log Files

"""
given an array of 'logs'. each log is a space delimited sting of words where
the first word is the identifier. there are two types of logs. letter logs
where all words consist of lowercase english letters, and digit logs where
all words consist of digits. reorder thesse logs so that the letter logs come
before the digit logs, the letter logs are sorted lexicographically by their
contents. if their contents are the same, then sort them lexicographically by
their identifiers, the digit logs maintain their relative order. return the
final order of the logs.
"""


class Solution(object):
    def reorderLogFiles(self, logs):
        """
        :type logs: List[str]
        :rtype: List[str]
        """
        digits, letters = [], []
        for l in logs:
            if l.split()[1].isdigit():
                digits.append(l)
            else:
                letters.append(l)
        letters.sort(key=lambda x: x.split()[1])
        letters.sort(key=lambda x: x.split()[1:])
        ans = letters + digits
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(
        obj.reorderLogFiles(
            logs=[
                "dig1 8 1 5 1",
                "let1 art can",
                "dig2 3 6",
                "let2 own kit dig",
                "let3 art zero",
            ]
        )
    )
    print(
        obj.reorderLogFiles(
            logs=[
                "a1 9 2 3 1",
                "g1 act car",
                "zo4 4 7",
                "ab1 off key dog",
                "a8 act zoo",
            ]
        )
    )