[PS] 파일명 정렬

owo·2023년 2월 3일
0

PS

목록 보기
13/25

문제 링크

코드

def solution(files):
    seperated_files = list(map(lambda x: seperate(x), files))
    sorted_files = sorted(seperated_files, key = lambda x: (x[0].lower(), int(x[1])))
    return list(map(lambda f: "".join(f), sorted_files))

def seperate(file): 
    n_start = -1
    n_end = len(file)
    for i, f in enumerate(file):
        if f.isdigit() and n_start == -1:
            n_start = i
        elif n_start != -1 and not f.isdigit():
            n_end = i
            break
    return (file[:n_start], file[n_start:n_end], file[n_end:])

리뷰

  • seperate() 함수는 정규표현식을 이용하면 더 쉽게 할 수 있다

0개의 댓글