Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

[2023-10-04] sumin #299 #329

Merged
merged 1 commit into from
Mar 18, 2024
Merged

[2023-10-04] sumin #299 #329

merged 1 commit into from
Mar 18, 2024

Conversation

ksumini
Copy link
Contributor

@ksumini ksumini commented Oct 9, 2023

PR Summary

풀이시간: 20분

<input>
files: 파일명(100글자 이내, 영문 대소문자, 숫자, 공백, 마침표, 빼기 부호만으로 이루어져있음), 1000개 이하의 파일

  • 영문자로 시작하며, 숫자를 하나 이상 포함
  • HEAD: 최소 한 글자 이상의 문자
  • NUMBER: 한 글자에서 최대 다섯글자 사이의 연속된 숫자(앞쪽에 0이 올 수 있음. 0~9999 사이의 숫자)
  • TAIL: 숫자가 다시 나타날 수도, 아무 글자도 없을 수도 있다.

<solution>
head, number, tail을 나눠주고 정렬

<시간복잡도>
O(1000 * 100): files의 크기 * file의 최대 길이

<회고>
head, number, tail을 일일히 나눠서 구현했는데..
다른 사람들 풀이를 보니 정규표현식으로 아주 깔끔하게 구현한 걸 확인..

[정규표현식 풀이]
정규표현식으로 구현할 때, number를 먼저 정렬하고, head를 정렬한다. 순서가 바뀌는 점 주의!
또, 파이썬 정렬에서 ['123', '0123']의 각 원소를 int로 변환해 정렬한다 했을 때, 같은 값이라면 순서도 유지된다는 걸 처음 알았음..

@ksumini ksumini self-assigned this Oct 9, 2023
@ksumini ksumini linked an issue Oct 9, 2023 that may be closed by this pull request
Copy link
Contributor

@zsmalla zsmalla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수민님 덕에 좋은 인사이트 얻어가는 것 같습니다! head,number,tail 나누는 것도 깔끔하게 잘 하신 것 같아요. 고생하셨습니다!

elif not tail and x.isdigit():
number += x
# 3) tail은 그 외 나머지 부분(숫자가 다시 나타날 수도, 아무 글자도 없을 수도 있음)
elif head and number:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이렇게 하면 따로 flag변수를 두지 않아도 되는 군요! 좋은 인사이트 얻어갑니다.


def solution(files):
# number 정렬 후 head를 정렬해야 한다.
files = sorted(files, key=lambda file: int(re.findall('\d+', file)[0]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와 정규표현식을 활용할 수 있겠다는 생각은 했지만, sort함수의 key로 두는 방법은 진짜 생각도 못했네요... 세상 사람들 왜이렇게 똑똑한거여

@zsmalla zsmalla merged commit 356ff83 into main Mar 18, 2024
@zsmalla zsmalla deleted the sumin-#299 branch March 18, 2024 10:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Programmers] 파일명 정렬
2 participants