- Reference
- μ«μμ λ¬Έμ λ°λ‘ μ μ₯νλ€λ κ²μ κΈ°μ΅νμ.
- λλ€ μ λ ¬μ μ νμ©νμ.
Solution
from typing import List
class Solution:
# logs: μ¬μ μμΌλ‘ μ λ ¬ (λ¬Έμ λ¨Όμ μ λ ¬λκ³ , μ«μλ μλμ μΈ μμ μ μ§)
# λ¬Έμμ μ«μμ μμ μ λ ¬ λ¬Έμ λκΉ lambda μ¨λ³΄μ!
def reorderLogFiles(self, logs: List[str]) -> List[str]:
letters = []
digits = []
for log in logs:
identifier, words = log.split(" ", 1)
if log.split()[1].isalpha():
letters.append((identifier, words))
else:
digits.append(log)
letters.sort(key=lambda x: (x[1], x[0]))
real_letters = []
for letter in letters:
real_letters.append(" ".join(letter))
return real_letters + digits
print(Solution().reorderLogFiles(["dig1 8 1 5 1", "let1 art can", "dig2 3 6", "let2 own kit dig", "let3 art zero"]))
Another Solution
class Solution:
def reorderLogFiles(self, logs: List[str]) -> List[str]:
letters, digits = [], []
for log in logs:
if log.split()[1].isdigit():
digits.append(log)
else:
letters.append(log)
# x.split()[1:] -> μ μμ μ λ ¬μΈ λ―? μ¬λΌμ΄μ±μΌλ‘ μ λ ¬νλ 건 μ²μ λ΄
letters.sort(key=lambda x: (x.split()[1:], x.split()[0]))
return letters + digits
Leave a comment