알고리즘/Problem Solving
[LeetCode] Reorder Log Files
도툐리
2021. 4. 11. 22:29
leetcode.com/problems/reorder-data-in-log-files/
Reorder Data in Log Files - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
0) 내가 처음에 제출한 코드
=> 못풀었음. (다음에 반드시 다시 한번 스스로 풀어볼것)
1) 파이썬 알고리즘 인터뷰 : 람다와 + 연산자를 이용
class Solution:
def reorderLogFiles(self, logs: List[str]) -> List[str]:
# convert a string separated by spaces to a list
letters, digits = [], []
for log in logs:
if log.split()[1].isalpha():
letters.append(log)
else:
digits.append(log)
# if letter --> alphabet order
# if alphabet order same, --> check the identifier to figure out the order
letters.sort(key=lambda x: (x.split()[1:], x.split()[0]))
# if digit --> input order remains
# letter > digit
return (letters + digits) # list concat
s.sort( key=lambda x : (x.split().[1], x.split().[0]) )
--> 이부분이 포인트!
정렬을 시키는데 key = lamda x: ( 첫째순위, 둘째순위, .... ) 의 형식을 이용해
내가 원하는 값을 key로 정렬할 수 있다!
내용 출처 :
책 <파이썬 알고리즘 인터뷰>