알고리즘
프로그래머스 Lv2 파일명 정렬 Python
스위태니
2023. 12. 11. 00:21
728x90
문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/17686
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
정답 코드
def solution(files):
answer = []
lenFiles = len(files)
newFiles = [[0 for _ in range(3)] for _ in range(lenFiles)]
for r in range(lenFiles):
file = files[r]
lenFile = len(file)
nowIndex = 0
HEAD = ""
NUMBER = ""
TAIL = ""
for f in range(nowIndex, lenFile):
tmp = file[f]
if tmp.isdigit():
nowIndex = f
break
HEAD += tmp
for s in range(nowIndex, lenFile):
tmp = file[s]
if tmp.isdigit():
NUMBER += tmp
if s == lenFile - 1:
nowIndex = s + 1
else:
nowIndex = s
break
TAIL = file[nowIndex:]
newFiles[r][0] = HEAD
newFiles[r][1] = NUMBER
newFiles[r][2] = TAIL
sortedNewFiles = sorted(newFiles, key=lambda x: (x[0].upper(), int(x[1])))
for sNF in sortedNewFiles:
answer.append(sNF[0] + sNF[1] + sNF[2])
return answer
풀이 방법
- 쉽게 말해서 머리, 숫자, 꼬리를 나눈다음에
- 정렬을 해준다. 근데 여기서 람다식의 upper라거나 int(x[1]) 이런거 사용해보면 된다.
느낀점
- 분명 더 좋은 방식이 있을거라 내가 풀 수 있는지 검증해본 문제다. 의외로 오래 걸려서 놀랐고 가볍게 보다가 문제를 잘 못 읽어서 틀릴 수 있기 때문에 주의해야 한다.
- 반복문을 계속 돌렸는데 브루트포스라고 봐도 되지 않나?
728x90