Amazon Online Assessment (OA) - Reorder Data in Log Files

You have an array of logs. Each log is a space-delimited string of words.

For each log, the first word in each log is an alphanumeric identifier. Then, either:

  • Each word after the identifier will consist only of lowercase letters, or;
  • Each word after the identifier will consist only of digits.

We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier.

Reorder the logs so that all of the letter-logs come before any digit-log. The letter-logs are ordered lexicographically ignoring identifiers, with the identifier used in case of ties. The digit-logs should be put in their original order.

Return the final order of the logs.

Input

The input consists of an argument:

logs: a list of strings representing the logs name

Output

Return list of the final order of the logs.

Constraints

0 <= logs.length <= 100

3 <= logs[i].length <= 100

logs[i] is guaranteed to have an identifier, and a word after the identifier

Examples

Example 1:

Input:

keywords = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]

Output: ["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]

Try it yourself

1
1
from typing import List
2
2
3
3
def reorderLogFiles(logs: List[str]) -> List[str]:
4
-
    # WRITE YOUR BRILLIANT CODE HERE
4
+
    alphas = []
5
+
    nums = []
6
+
    for log in logs:
7
+
        ident, cont = log.split(' ', 1)
8
+
        (alphas if cont[0].isalpha() else nums).append((cont, ident))
9
+
    alphas.sort()
10
+
    return [f'{i} {c}' for c, i in alphas + nums]
11
+
5
12
if __name__ == "__main__":
6
13
    rows = int(input())
7
14
    logs = [input() for _ in range(rows)]
8
15
    result = reorderLogFiles(logs)
Expand 2 lines ...