Facebook Pixel

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

1def reorder_log_files(logs: list[str]) -> list[str]:
2    alphas: list[tuple[str, str]] = []
3    nums: list[tuple[str, str]] = []
4    for log in logs:
5        ident, cont = log.split(" ", 1)
6        (alphas if cont[0].isalpha() else nums).append((cont, ident))
7    alphas.sort()
8    return [f"{i} {c}" for c, i in alphas + nums]
9
10if __name__ == "__main__":
11    logs = [input() for _ in range(int(input()))]
12    res = reorder_log_files(logs)
13    for line in res:
14        print(line)
15
Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro
Favorite (idle)