728x90

나의 풀이
import re
from collections import defaultdict
def solution(files):
answer = []
data = defaultdict(list)
pattern = re.compile(r'^(?P<head>\D+)(?P<number>\d{1,5})')
for idx, file in enumerate(files):
m = pattern.search(file)
if m:
data[m.group('head').lower()].append((int(m.group('number')), idx, file))
for head, files in sorted(data.items()):
for file in sorted(files):
answer.append(file[-1])
return answer
간결한 풀이
import re
def solution(files):
a = sorted(files, key=lambda file : int(re.findall('\d+', file)[0]))
b = sorted(a, key=lambda file : re.split('\d+', file.lower())[0])
return b
- 숫자의 개수나 tail 부분 처리만 해주면 더 좋을 듯
반응형
'Coding Test > Programmers' 카테고리의 다른 글
| [프로그래머스/파이썬] [3차] n진수 게임 (0) | 2022.06.20 |
|---|---|
| [프로그래머스/파이썬] 올바른 괄호 (0) | 2022.06.20 |
| [프로그래머스/파이썬] [3차] 압축 (0) | 2022.06.18 |
| [프로그래머스/파이썬] 가장 큰 정사각형 찾기 (0) | 2022.06.18 |
| [프로그래머스/파이썬] [3차] 방금그곡 (0) | 2022.06.18 |