Amazon Online Assessment (OA) 2021 - Reorder Data in Log Files | Upgrading Junction Boxes | HackerRank

Practice on leetcode here: https://leetcode.com/problems/reorder-data-in-log-files/

Solution

1
1
from typing import List
2
2
3
3
def reorder_log_files(logs: List[str]) -> List[str]:
4
-
    # WRITE YOUR BRILLIANT CODE HERE
4
+
    alphas = []
5
-
    return []
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
+
6
12
if __name__ == '__main__':
7
13
    logs = [input() for _ in range(int(input()))]
8
14
    res = reorder_log_files(logs)
9
15
    for line in res:
10
16
        print(line)