Coding Test/프로그래머스

[프로그래머스] / [Level 2] / [Python] [3차] 파일명 정렬

욱근욱 2023. 3. 27. 16:43

https://school.programmers.co.kr/learn/courses/30/lessons/17686

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


def solution(files):
    answer = []
    an = []

    for file in files:
        s = []
        for idx, i in enumerate(file):    
            if i.isdigit():
                s.append(idx)
            elif len(s) > 0 and i.isdigit()==False:
                break
            
        head = file[:s[0]]
        number = file[s[0]:s[-1]+1]        
        etc = file[s[-1]+1:]
        
        an.append([head, number, etc])
        
    an = sorted(an, key=lambda x: (x[0].lower(), int(x[1])))

    answer = [''.join(a) for a in an]
    
    return answer

다음 방법을 쓰면 더 쉽게 구할 수 있다.

import re
re.findall(r'\d+', file)