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

1from typing import List, Tuple
2
3def reorder_log_files(logs: List[str]) -> List[str]:
4    alphas: List[Tuple[str, str]] = []
5    nums: List[Tuple[str, str]] = []
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
12if __name__ == "__main__":
13    logs = [input() for _ in range(int(input()))]
14    res = reorder_log_files(logs)
15    for line in res:
16        print(line)
17

Got a question?ย Ask the Monster Assistantย anything you don't understand.

Still not clear? ย Submitย the part you don't understand to our editors. Or join ourย Discord and ask the community.

โ†
โ†‘๐Ÿช„