Published:
Updated:

  • 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