aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--reorder_data_log_files.c45
-rw-r--r--reorder_data_log_files.py56
2 files changed, 101 insertions, 0 deletions
diff --git a/reorder_data_log_files.c b/reorder_data_log_files.c
new file mode 100644
index 0000000..18c4b88
--- /dev/null
+++ b/reorder_data_log_files.c
@@ -0,0 +1,45 @@
+// 937. Reorder Data in Log Files
+#include "leetcode.h"
+
+/*
+ * 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.
+ */
+
+int cmp(const void *x, const void *y) {
+ const char *s1 = *(const char **)x, *s2 = *(const char **)y;
+ const char *sp1 = strchr(s1, ' '), *sp2 = strchr(s2, ' ');
+ bool a1 = isalpha(*++sp1), a2 = isalpha(*++sp2);
+ int r = 0, l = sp1 - s1;
+ return a1 && a2 ? !(r = strcmp(sp1, sp2)) ? strncmp(s1, s2, l) : r : a2 - a1;
+}
+
+char **reorderLogFiles(char **logs, int logsSize, int *returnSize) {
+ qsort(logs, logsSize, sizeof(char *), cmp);
+ return *returnSize = logsSize, logs;
+}
+
+int main() {
+ char *l1[] = {"dig1 8 1 5 1", "let1 art can", "dig2 3 6", "let2 own kit dig",
+ "let3 art zero"};
+ char *l2[] = {"a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog",
+ "a8 act zoo"};
+ int rs1, rs2;
+ char **rlf1 = reorderLogFiles((char **)l1, ARRAY_SIZE(l1), &rs1);
+ char **rlf2 = reorderLogFiles((char **)l2, ARRAY_SIZE(l2), &rs2);
+ for (int i = 0; i < rs1; i++)
+ printf("%s ", rlf1[i]); // expect: "let1 art can","let3 art zero","let2 own
+ // kit dig","dig1 8 1 5 1","dig2 3 6"
+ printf("\n");
+ for (int i = 0; i < rs2; i++)
+ printf("%s ", rlf2[i]); // expect: "g1 act car","a8 act zoo","ab1 off key
+ // dog","a1 9 2 3 1","zo4 4 7"
+ printf("\n");
+ free(rlf1), free(rlf2);
+}
diff --git a/reorder_data_log_files.py b/reorder_data_log_files.py
new file mode 100644
index 0000000..d6da9f7
--- /dev/null
+++ b/reorder_data_log_files.py
@@ -0,0 +1,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",
+ ]
+ )
+ )