일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 공선옥
- 스프링 시스템 구조
- 리트허브 사용법
- 주니어개발자
- 알고리즘
- 개발자 취준
- collection python
- Programmers 배열의 유사도
- 비지니스 레이어
- 리트허브 오류
- 프로그래머스 가위바위보 풀이
- 프로그래머스 특정 문자 제거하기
- 프레젠테이션 레이어
- 프로그래머스 배열의 유사도 파이썬
- 리트허브 커밋
- 프로그래머스 배열 회전시키기
- 파이썬 컬렉션
- 파이썬 특정 문자 제거하기
- leftJoin
- programmers 배열 회전
- 2-layered architecture
- BigO notation
- 가위바위보 풀이
- 춥고더운우리집
- 춥고 더운 우리 집
- 특정문자 제거하기
- 프로그래머스
- 배열의 유사도 파이썬
- 슈츠 자막
- 프로그래머스 가위바위보
- Today
- Total
기억보다 기록을
[leetcode] Reorder Data in Log Files (python) 본문
Q. You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.
There are two types of logs:
- Letter-logs: All words (except the identifier) consist of lowercase English letters.
- Digit-logs: All words (except the identifier) consist of digits.
Reorder these logs so that:
- The letter-logs come before all digit-logs.
- The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
- The digit-logs maintain their relative ordering.
Return the final order of the logs.
Idea: :
The first word is identifier, Order: letters(sorted lexicographically) + digits(maintaining original order)
1. check each word to check whether letters or digits
2. sorting it without Identifier
3. add digits array after letters array
-배열 인덱스 1부터 체크해야겠다
-배열 2개를 선언한다 하나는 letters를 담을거고 하나는 digits 모으기
-그럼 우선 문자열/ 정수 인지 구분이 필요하다 -> isalpha 함수 써보자
A.
if문에 log.split()[1]가 유효한 이유는, 배열이 숫자+문자 조합이 아니라(식별자 제외), 문자열끼리/ 숫자열끼리로 나누어져있기 때문이다.
그래서 line5에서 인덱스1에 해당하는 값만을 체크한다.
digits 리스트는 더이상 체크하지 않고 마지막에 + 연산자로 맨 뒤에 추가한다
letters 리스트를 어떻게 정렬할지가 중요하한데 여기서 람다써서 하는게 kick point
class Solution:
def reorderLogFiles(self, logs: List[str]) -> List[str]:
letters , digits = [], []
for log in logs:
if log.split()[1].isalpha():
letters.append(log)
else:
digits.append(log)
letters.sort(key = lambda x: (x.split()[1:], x.split()[0]))
return letters + digits
람다 참고:
list.sort(key = lambda x : (x[0], x[1]))
비교할 요소가 2개. x[0]으로 오름차순 정렬하고, 그 다음에 x[1]로 정렬
-> x[0]이 같으면, x[1]을 기준으로 정렬
'Algorithm > Leet code' 카테고리의 다른 글
[Leetcode] two sum을 4가지 방법으로 풀어보기 (python) (0) | 2023.04.27 |
---|---|
[Leetcode] Number of Islands (BFS 풀이) (0) | 2023.04.26 |
[Leetcode] Number of Islands (python) , dfs 풀이 (0) | 2023.04.17 |
[leetcode] Most Common Word (0) | 2023.02.22 |
[leetcode] Valid Palindrome (python) (0) | 2023.02.21 |