취준시절/프로그래머스

[프로그래머스] 2018 KAKAO BLIND RECRUITMENT - [3차] 파일명 정렬

MAYMIN 2021. 9. 8. 23:16
728x90
SMALL

 


    x[1].lower()로 비교한 이유 !!!

 

만약 x[1].lower()가 아닌 x[01로 정렬할 경우 "img1.png", "IMG01.GIF" 가 "IMG01.GIF","img1.png"로 출력된다
 

문제에서 head가 대소문자 제외시 같고 number도 같다면 입력된 순서대로 그대로 출력해야한다고했다.

그래서 대소문자 구분없으니까 애초에 대소문자 무력화시키기위해 .lower()나 .upper()로 정렬시작한다.
    

def solution(files):
    split_file = []
    for f in files:
        head = ''
        number = ''
        numcheck = False
        for i in f:
            if i.isdigit():
                number += i
                numcheck = True
            elif numcheck == False:
                head += i
            else:
                break
        split_file.append((f, head, number)) # f 는 원래 파일명 , head랑 number만 비교
    split_file.sort(key=lambda x: (x[1].lower(), int(x[2])))
    split_file = [f[0] for f in split_file] # head랑 number로 정렬 해두고 원래 파일명으로 결과값 만들기

    return split_file
728x90
LIST